【问题标题】:Cannot start service via Ansible on WSL 2 Ubuntu20.04无法在 WSL 2 Ubuntu20.04 上通过 Ansible 启动服务
【发布时间】:2021-09-03 02:35:48
【问题描述】:

我正在尝试使用 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 系统的信息:
unname -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 devops systemd ubuntu-20.04 wsl-2


    【解决方案1】:

    实际上您遇到了两个问题。首先,您似乎在专门使用Ansible systemd module。这是行不通的,因为 WSL 不支持 systemd 需要付出大量努力。

    正如您所注意到的,由于 WSL Ubuntu 20.04 发行版,确实提供了对 service 命令的回退,您应该能够简单地将其换掉与Ansible service module。例如:

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

    但是你会遇到第二个问题——WSL 不提供任何类型的“启动脚本”支持。正如我们已经提到的,不支持 systemd,但旧的 SysVInit 也不支持。 WSL 使用它自己的 init 系统作为 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 之类的内容。

    【讨论】:

    • 感谢您的回答。从 systemd 更改为 service 解决了我的问题。下一步是在 WSL Ubuntu 启动后自动运行服务,但您的回答也会很有帮助。
    猜你喜欢
    • 1970-01-01
    • 2020-12-31
    • 2018-11-18
    • 2023-03-31
    • 2020-10-06
    • 2023-01-15
    • 2016-04-29
    • 1970-01-01
    • 2022-01-08
    相关资源
    最近更新 更多