简体   繁体   English

无法在 WSL 2 Ubuntu20.04 上通过 Ansible 启动服务

[英]Cannot start service via Ansible on WSL 2 Ubuntu20.04

I am trying to run Ansible playbook on WSL 2 with Ubuntu20.04.我正在尝试使用 Ubuntu20.04 在 WSL 2 上运行 Ansible playbook。 Majority of tasks work properly, however all tasks which manage the services (eg start nginx) fails.大多数任务正常工作,但是管理服务的所有任务(例如启动 nginx)都失败了。

Ansible code: Ansible代码:

  - 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:关于 WSL 2 系统的信息:
uname -a:用户名 -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: 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 --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 --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.当我通过命令启动服务时: sudo service nginx start - 它工作正常。

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 .首先,您似乎专门使用Ansible systemd 模块 That's not going to work, since WSL doesn't support systemd without extensive effort.这是行不通的,因为 WSL 不经过大量努力就不会支持 systemd。

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 .正如您所注意到的,由于 WSL Ubuntu 20.04 发行版确实提供了对service命令的回退,因此您应该能够简单地将其替换为Ansible 服务模块 Eg:例如:

- 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.但是接下来您将遇到第二个问题——WSL 不提供任何类型的“启动脚本”支持。 As we've already mentioned, systemd isn't supported, but neither is the older SysVInit.正如我们已经提到的,systemd 不受支持,但旧的 SysVInit 也不支持。 WSL uses it's own init system as PID 1 to bootstrap the interoperability between the native Windows services and WSL/Linux. WSL 使用它自己的初始化系统作为 PID 1 来引导原生 Windows 服务和 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:问题真的是你想什么时候启动服务:

  • When Windows boots? Windows何时启动?
  • When the user logs in?用户何时登录?
  • When the WSL instance is started for the first time manually (through, say, a Windows Terminal invocation)?首次手动启动 WSL 实例时(例如,通过 Windows 终端调用)?

The first two should be possible with a Windows Task Manager script that runs something like wsl -u root service nginx start .前两个应该可以使用运行wsl -u root service nginx start类的 Windows 任务管理器脚本。 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.如果 WSL 实例不是在用户会话中启动,我已经注意到 Windows 终止 WSL 实例的一些问题。 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:在第一次 WSL 调用时启动nginx将涉及:

  • Setting up sudoers (through visudo , of course) to allow your user to run sudo service nginx start without a password.设置sudoers (当然是通过visudo )以允许您的用户在没有密码的情况下运行sudo service nginx start
  • Editing your startup files (eg .bash_profile ) to include something like service nginx status || sudo service nginx start编辑您的启动文件(例如.bash_profile )以包含诸如service nginx status || sudo service nginx start service nginx status || sudo service nginx start . service nginx status || sudo service nginx start

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM