简体   繁体   English

使用 WSL 中 Localhost 上的 Ansible 模块服务启动 Apache2 失败 Ubuntu 20.04.3 LTS(焦点)

[英]Failed to start Apache2 using Ansible Module Service on Localhost in WSL Ubuntu 20.04.3 LTS (focal)

I tried to start Apache2 service using 'sudo service' command and it works, but when I tried to use ansible service module to start the apache2, I got the error message saying "Service is in unknown state".我尝试使用 'sudo service' 命令启动 Apache2 服务并且它有效,但是当我尝试使用 ansible 服务模块启动 apache2 时,我收到了未知错误消息。

I do upgraded my ansible to the latest version.我确实将我的 ansible 升级到了最新版本。 I am working in WSL Ubuntu 20.04.3LTS.我在 WSL Ubuntu 20.04.3LTS 工作。 And I do have set up the SSH correctly so that i can run other ansible ad-hoc command using modules like apt, command and shell, etc.而且我确实已经正确设置了 SSH,这样我就可以使用 apt、command 和 shell 等模块运行其他 ansible ad-hoc 命令。

Any clue where the problem may be?任何线索可能是问题所在?

After digging a little further, I found that seems like ansible has a bug in service_mgr.py at the method 'is_systemd_managed_offline(module), which makes ansible think the service manager is systemd while it is actually service (or sysv init):在进一步挖掘之后,我发现 ansible 在方法 'is_systemd_managed_offline(module) 处的 service_mgr.py 中似乎有一个错误,这使得 ansible 认为服务管理器是 systemd,而它实际上是服务(或 sysv init):

if module.get_bin_path('systemctl'):
            # check if /sbin/init is a symlink to systemd
            # on SUSE, /sbin/init may be missing if systemd-sysvinit package is not installed.
            if os.path.islink('/sbin/init') and os.path.basename(os.readlink('/sbin/init')) == 'systemd':
                return True
        return False

This is because on my WSL Ubuntu 20.04 LTS, there is indeed a link from /sbin/init to systemd, as shown:这是因为在我的 WSL Ubuntu 20.04 LTS 上,确实存在从 /sbin/init 到 systemd 的链接,如图所示:

/sbin/init -> /lib/systemd/systemd*

However, the service manager actually should be set to sysv init, which would be set at the end of the elif part of code in service_mgr.py if everything works normally:但是,服务管理器实际上应该设置为 sysv init,如果一切正常,它将设置在 service_mgr.py 中 elif 部分代码的末尾:

        elif collected_facts.get('ansible_system') == 'Linux':
            # FIXME: mv is_systemd_managed
            if self.is_systemd_managed(module=module):
                service_mgr_name = 'systemd'
            elif module.get_bin_path('initctl') and os.path.exists("/etc/init/"):
                service_mgr_name = 'upstart'
            elif os.path.exists('/sbin/openrc'):
                service_mgr_name = 'openrc'
            elif self.is_systemd_managed_offline(module=module):
                service_mgr_name = 'systemd'
            elif os.path.exists('/etc/init.d/'):
                service_mgr_name = 'sysvinit'

        if not service_mgr_name:
            # if we cannot detect, fallback to generic 'service'
            service_mgr_name = 'service'

However, due to the existing link shown above, ansible stop at the elif line of is_system_managed_offline and will not continue to the end of the elif series.但是,由于上面显示的现有链接,ansible 在 is_system_managed_offline 的 elif 行停止,不会继续到 elif 系列的末尾。

As shown here below, the service manager is shown to be init (sysv init):如下图所示,服务管理器显示为 init (sysv init):

$ ps -p 1 -o comm=
init

Any clue how to work this around?任何线索如何解决这个问题?

//////////IT IS SOLVED/////////////////// By checking the documentation of service module on ansible docs website, and using the argument 'use' to force ansible to use 'service' instead of 'systemd' as service manager (although from ansible setup module, ansible still think wrongly that systemd is the service manager of my WSL Ubuntu 20.04.3 LTS, because of code bug as shown above. /////////IT IS SOLVED/////////////////通过查看ansible docs网站上的服务模块文档,并使用参数'use ' to force ansible to use 'service' instead of 'systemd' as service manager (although from ansible setup module, ansible still think wrongly that systemd is the service manager of my WSL Ubuntu 20.04.3 LTS, because of code bug as shown above .

Hope in the future version of Ansible, the code bug (of elif for checking service manager available on the Linux machine) above can be solved, especially by taking the WSL Ubuntu and other WSL variant into account.希望在 Ansible 的未来版本中,可以解决上述代码错误(用于检查 Linux 机器上可用的服务管理器的 elif 的代码错误),特别是通过考虑 WSL Z3D945423F8E9496C429A5D8C65B4 变体和其他。60

A quick fix would be to rearrange the elif order to put the 'is_systemd_managed_offline' variant at the end of the elif list and move os.path.exists('/etc/init.d') higher in the list, as shown:一个快速的解决方法是重新排列 elif 顺序,将 'is_systemd_managed_offline' 变体放在 elif 列表的末尾,并将 os.path.exists('/etc/init.d') 在列表中移到更高的位置,如下所示:

    elif collected_facts.get('ansible_system') == 'Linux':
            # FIXME: mv is_systemd_managed
            if self.is_systemd_managed(module=module):
                service_mgr_name = 'systemd'
            elif module.get_bin_path('initctl') and os.path.exists("/etc/init/"):
                service_mgr_name = 'upstart'
            elif os.path.exists('/sbin/openrc'):
                service_mgr_name = 'openrc'
            *elif os.path.exists('/etc/init.d/'):
                service_mgr_name = 'sysvinit'
            elif self.is_systemd_managed_offline(module=module):
                service_mgr_name = 'systemd'*

I think you can force your service module to use "service" instead of systemctl:我认为您可以强制您的服务模块使用“服务”而不是 systemctl:

ansible  localhost -m service -a "use=service name=apache2 state=stopped"

Service use reference 服务使用参考

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

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