I got this error :
TASK [Gathering Facts] *************************************************************************************************
fatal: [mem1]: FAILED! => {“msg”: “The powershell shell family is incompatible with the sudo become plugin”}
ok: [dc1]
Are you trying to install Nginx on a Windows machine or an Ubuntu/Debian machine? You’ve got a bunch of mixed terms going on.
For Linux, the following should work on most distributions, not just Ubuntu/Debian.
---
- name: Install Nginx - Linux
hosts: all
become: true
# become_method: sudo ## this is the default become_method
tasks:
- name: Install Nginx
ansible.builtin.package: # Linux package manager agnostic
name: nginx
state: present
- name: Start Nginx
ansible.builtin.service:
name: nginx
state: started
enabled: true
For Windows:
I’m just showing you what you might for Windows use since apt is a Debian package manager, not a Windows one. Not everyone is comfortable with using third party packagers for Windows, so you might need to download the Windows build, and configure everything (including a service) yourself.
---
- name: Install Nginx - Windows
hosts: all
become: true
become_method: runas
tasks:
- name: Install Nginx
chocolatey.chocolatey.win_chocolatey:
name: nginx
state: present
- name: Start Nginx
ansible.windows.win_service:
name: nginx
state: started
state_mode: auto
Essential, for this to work you would want WSL v2 using Ubuntu 20.04 at a minimum for the services module to work the way you and him are trying to use it.
Since docker-desktop is running on WSL v2, we know that v2 is installed, but your Ubuntu instance is still running on v1.
wsl --set-version Ubuntu 2 will fix the WSL version it’s running on. Then go in and modify the /etc/wsl.conf file to start systemd on boot. Restart your instance to make sure changes take effect, and then run systemctl status nginx to see if you get any kind of systemd error.
Then try running your playbook again.
You might also want to run wsl --set-default-version 2 to make sure all future instances default to v2.
Windows CMD: Shutdown your current WSL Instance wsl --shutdown
Windows CMD: Convert your Ubuntu instance to WSL 2 wsl --set-version Ubuntu 2
Windows CMD: Start your Ubuntu instance again wsl -d Ubuntu
If you use the service module in your Ansible playbook, I believe it should work. If not, let us know. You may still need to do more modifications to your Ubuntu system files and restart WSL again.
According to the Stack Overflow link I posted above, Ubuntu 20.04 may have a built in fall-back mechanism to handle the ansible.built-in.service module. So, enabling systemd MAY not be necessary.
I’d say, convert the WSL Ubuntu Distribution to version 2, test, and if it still doesn’t work then try enabling systemd.
I believe in the least number of changes necessary to make something new work.
It’s been a while since I’ve played with WSL + SystemD, so it may or may not be enough. According to my notes from the last time I actually played with it, I used Distrod to install Fedora 36 with SystemD. Then my manager found out and had a sandbox VM created for me to use instead, lol. So, I don’t remember if SystemD worked with just vanilla WSL v2, or if Distrod was required. ¯\_(ツ)_/¯
I mainly remember using Distrod so I could use Fedora, not because of the SystemD support.