繁体   English   中英

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

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

我正在尝试使用 Ubuntu20.04 在 WSL 2 上运行 Ansible playbook。 大多数任务正常工作,但是管理服务的所有任务(例如启动 nginx)都失败了。

Ansible代码:

  - name: NGINX - enable and start nginx service
    systemd:
      name: nginx
      enabled: yes
      state: started

我在命令行中收到以下错误:

TASK [ansible-role-nginx : NGINX - enable and start nginx service]
fatal: [webapp]: FAILED! => {"changed": false, "msg": "Service is in unknown state", "status": {}}

关于 WSL 2 系统的信息:
用户名 -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

当我通过命令启动服务时: sudo service nginx start - 它工作正常。

如果您对此案例有任何建议或解决方案,请告诉我。

实际上,您遇到了两个问题。 首先,您似乎专门使用Ansible systemd 模块 这是行不通的,因为 WSL 不经过大量努力就不会支持 systemd。

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

- name: NGINX - enable and start nginx service
    service:
      name: nginx
      enabled: yes
      state: started

但是接下来您将遇到第二个问题——WSL 不提供任何类型的“启动脚本”支持。 正如我们已经提到的,systemd 不受支持,但旧的 SysVInit 也不支持。 WSL 使用它自己的初始化系统作为 PID 1 来引导原生 Windows 服务和 WSL/Linux 之间的互操作性。

所以“启用”标志不会做任何事情。

问题真的是你想什么时候启动服务:

  • Windows何时启动?
  • 用户何时登录?
  • 首次手动启动 WSL 实例时(例如,通过 Windows 终端调用)?

前两个应该可以使用运行wsl -u root service nginx start类的 Windows 任务管理器脚本。 “登录”绝对是一个更简单的用例。 如果 WSL 实例不是在用户会话中启动,我已经注意到 Windows 终止 WSL 实例的一些问题。 如果您遇到这种情况,有一种可能的解决方法,但我建议单独提出一个问题/答案来涵盖它。

在第一次 WSL 调用时启动nginx将涉及:

  • 设置sudoers (当然是通过visudo )以允许您的用户在没有密码的情况下运行sudo service nginx start
  • 编辑您的启动文件(例如.bash_profile )以包含诸如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