gpt4 book ai didi

docker - Ansible 服务重启不起作用

转载 作者:行者123 更新时间:2023-12-02 18:22:29 24 4
gpt4 key购买 nike

我使用 Ansible 来提供带有 Vagrant 的 Docker 容器。

我的 Ansible 文件包含以下部分以启动 nginx:

- name: nginx started
service:
name: nginx
state: restarted

据我了解,这部分应该在所有情况下重新启动 nginx,但是当我通过 SSH 连接到容器时, nginx没有运行(整个 Ansible 提供过程成功, /var/log/nginx/error.log 中没有 nginx 的日志)。当我手动输入以下命令时,它会正确启动: sudo service nginx start .

但是,当我将上面的部分替换为:
- name: nginx restarted
command: service nginx restart

看来问题不仅限于 nginx ,并且也会发生在其他服务中,例如 syslog-ng .

知道为什么要使用 Ansible service不工作? ( docker 17.10.0, Vagrant 2.0.1,Ansible 2.4.0)

最佳答案

Ansible service模块 try to guess the underlying init system .
在您的情况下 phusion/baseimage docker 图像,it finds /sbin/initctl ,因此该模块只需启动 /sbin/initctl stop nginx; /sbin/initctl start nginxdoes nothing 的容器内因为此图像中的初始化系统已更改( my_init )。

所以问题是 ansible 没有正确检测到的图像的 init 系统状态不一致。

解决方案是:

  • 写一个my_init ansible模块(服务模块先尝试使用{{ ansible_service_mgr }}模块[code])
  • 删除 initctl从图像中,所以 ansible 不会检测到任何 init 系统并将使用 service命令(可能会在 phusion/baseimage-docker 中引发问题)
  • 使用 command明确使用 service 的模块命令就像你最后做的
  • 关于docker - Ansible 服务重启不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47420361/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com