I am trying to run Ansible playbook on WSL 2 with Ubuntu20.04. Majority of tasks work properly, however all tasks which manage the services (e.g. start nginx) fails.
Ansible code:
- name: NGINX - enable and start nginx service
systemd:
name: nginx
enabled: yes
state: started
I get the following error in command line:
TASK [ansible-role-nginx : NGINX - enable and start nginx service]
fatal: [webapp]: FAILED! => {"changed": false, "msg": "Service is in unknown state", "status": {}}
Info about WSL 2 system:
uname -a:
Linux CPX-TRWO066I0YQ 5.4.72-microsoft-standard-WSL2 #1 SMP Wed Oct 28 23:40:43 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
lsb_release -a:
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 20.04.2 LTS
Release: 20.04
Codename: focal
ansible --version:
ansible 2.9.16
config file = /etc/ansible/ansible.cfg
configured module search path = ['/home/ubuntu/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/local/lib/python3.8/dist-packages/ansible
executable location = /usr/local/bin/ansible
python version = 3.8.5 (default, May 27 2021, 13:30:53) [GCC 9.3.0]
systemd --version:
systemd 245 (245.4-4ubuntu3.6)
+PAM +AUDIT +SELINUX +IMA +APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ +LZ4 +SECCOMP +BLKID +ELFUTILS +KMOD +IDN2 -IDN +PCRE2 default-hierarchy=hybrid
When I start service by command: sudo service nginx start
- it works properly.
Please, let me know if you have any suggestions or solutions for this case.
There are actually two problems you are running into. First, it looks like you are specifically using the Ansible systemd module. That's not going to work, since WSL doesn't support systemd without extensive effort.
Since the WSL Ubuntu 20.04 distribution, as you've noticed, does provide a fallback to the service
command, you should be able to simply swap that out with the Ansible service module. E.g.:
- name: NGINX - enable and start nginx service
service:
name: nginx
enabled: yes
state: started
But then you are going to run into the second issue -- WSL doesn't provide any sort of "startup script" support. As we've already mentioned, systemd isn't supported, but neither is the older SysVInit. WSL uses it's own init system as PID 1 to bootstrap the interoperability between the native Windows services and WSL/Linux.
So the "enabled" flag just isn't going to do anything.
The question is really when do you want to start the service:
The first two should be possible with a Windows Task Manager script that runs something like wsl -u root service nginx start
. The "logs in" is definitely an easier use-case. I've noticed some issues with Windows terminating the WSL instance if it isn't started inside a user session. If you run into that, there's a possible workaround, but I'd suggest a separate question/answer to cover it.
Starting nginx
on first WSL invocation would involve:
sudoers
(through visudo
, of course) to allow your user to run sudo service nginx start
without a password..bash_profile
) to include something like service nginx status || sudo service nginx start
.