使用基于 rpm 的发行版和 systemd 作为 Docker 容器测试 ansible playbook
Test ansible playbook using rpm-based distros with systemd as Docker containers
我正在为 Ansible.Galaxy 编写 ansible 剧本,它将 install nginx 用于任何 Linux 发行版。
但我想用 Travis CI 和 Docker 在包含 Systemd 的图像上对其进行测试。因为SystemD报错:
Failed to get D-Bus connection: No connection to service manager.
如你所见https://travis-ci.org/weldpua2008/ansible-nginx/jobs/78864352
有错误。
所以我尝试表示它:
~# git clone https://github.com/weldpua2008/ansible-nginx.git
~# cd ansible-nginx
~# docker run -ti --rm=true -v `pwd`:/ansible-nginx:rw centos:7 /bin/bash ^C
~# cd ansible-nginx/
~/ansible-nginx# docker run -ti --rm=true -v `pwd`:/ansible-nginx:rw centos:7 /bin/bash
[root@2f6955a46b42 /]# /ansible-nginx/tests/test-on-rpm.sh
TASK: [ansible-nginx | create document root directory if needed] **************
ok: [localhost]
TASK: [ansible-nginx | Starting nginx service] ********************************
failed: [localhost] => {"failed": true}
msg: no service or tool found for: nginx
FATAL: all hosts have already failed -- aborting
PLAY RECAP ********************************************************************
to retry, use: --limit @/root/test.retry
localhost : ok=5 changed=2 unreachable=0 failed=1
可能没有这项服务?
[root@2f6955a46b42 /]# systemctl enable nginx.service
[root@2f6955a46b42 /]# systemctl -t service -a
Failed to get D-Bus connection: No connection to service manager.
[root@2f6955a46b42 /]# chkconfig --list
Note: This output shows SysV services only and does not include native
systemd services. SysV configuration data might be overridden by native
systemd configuration.
If you want to list systemd services use 'systemctl list-unit-files'.
To see services enabled on particular target use
'systemctl list-dependencies [target]'.
这就是我尝试在 ansible 中启动服务的方式:https://github.com/weldpua2008/ansible-nginx/blob/master/tasks/main-centos.yml#L24
- name: Starting nginx service
service: name=nginx state=started
tags:
- nginx
这是由于 Docker(普通 "unprivileged")容器没有 运行 systemd
的权限(因为它想要访问/sys/fs/cgroup
文件系统)。
但这实际上是因为 Docker 容器更喜欢 运行 一个进程,即您尝试使用它们的进程 - 将它们视为 "a Unix process with a bunch of supporting utilities",而不是 "mini-VMs".
你需要一个简单的 Docker 继承自 Centos 的文件,它安装 nginx
然后有一个最终的 CMD
就像这样:
CMD /usr/sbin/nginx -g "daemon off;"
有关提示,这里是官方 nginx
Docker 文件 https://github.com/nginxinc/docker-nginx/blob/7f3ef0927ec619d20181e677c97f991df0d7d446/Dockerfile
我正在为 Ansible.Galaxy 编写 ansible 剧本,它将 install nginx 用于任何 Linux 发行版。 但我想用 Travis CI 和 Docker 在包含 Systemd 的图像上对其进行测试。因为SystemD报错:
Failed to get D-Bus connection: No connection to service manager.
如你所见https://travis-ci.org/weldpua2008/ansible-nginx/jobs/78864352 有错误。
所以我尝试表示它:
~# git clone https://github.com/weldpua2008/ansible-nginx.git
~# cd ansible-nginx
~# docker run -ti --rm=true -v `pwd`:/ansible-nginx:rw centos:7 /bin/bash ^C
~# cd ansible-nginx/
~/ansible-nginx# docker run -ti --rm=true -v `pwd`:/ansible-nginx:rw centos:7 /bin/bash
[root@2f6955a46b42 /]# /ansible-nginx/tests/test-on-rpm.sh
TASK: [ansible-nginx | create document root directory if needed] **************
ok: [localhost]
TASK: [ansible-nginx | Starting nginx service] ********************************
failed: [localhost] => {"failed": true}
msg: no service or tool found for: nginx
FATAL: all hosts have already failed -- aborting
PLAY RECAP ********************************************************************
to retry, use: --limit @/root/test.retry
localhost : ok=5 changed=2 unreachable=0 failed=1
可能没有这项服务?
[root@2f6955a46b42 /]# systemctl enable nginx.service
[root@2f6955a46b42 /]# systemctl -t service -a
Failed to get D-Bus connection: No connection to service manager.
[root@2f6955a46b42 /]# chkconfig --list
Note: This output shows SysV services only and does not include native
systemd services. SysV configuration data might be overridden by native
systemd configuration.
If you want to list systemd services use 'systemctl list-unit-files'.
To see services enabled on particular target use
'systemctl list-dependencies [target]'.
这就是我尝试在 ansible 中启动服务的方式:https://github.com/weldpua2008/ansible-nginx/blob/master/tasks/main-centos.yml#L24
- name: Starting nginx service
service: name=nginx state=started
tags:
- nginx
这是由于 Docker(普通 "unprivileged")容器没有 运行 systemd
的权限(因为它想要访问/sys/fs/cgroup
文件系统)。
但这实际上是因为 Docker 容器更喜欢 运行 一个进程,即您尝试使用它们的进程 - 将它们视为 "a Unix process with a bunch of supporting utilities",而不是 "mini-VMs".
你需要一个简单的 Docker 继承自 Centos 的文件,它安装 nginx
然后有一个最终的 CMD
就像这样:
CMD /usr/sbin/nginx -g "daemon off;"
有关提示,这里是官方 nginx
Docker 文件 https://github.com/nginxinc/docker-nginx/blob/7f3ef0927ec619d20181e677c97f991df0d7d446/Dockerfile