使用基于rpm的发行版和systemd as Docker容器测试ansible playbook

使用基于rpm的发行版和systemd as Docker容器测试ansible playbook,ansible,travis-ci,ansible-playbook,Ansible,Travis Ci,Ansible Playbook,我正在为ansible.Galaxy编写ansible剧本,它将在任何Linux发行版上运行。 但我觉得可以用Travis CI和Docker在包含Systemd的图像上进行测试。因为SystemD给出了一个错误: 无法获取D总线连接:未连接到service manager 如你所见 有错误 因此,我试图表示: ~# git clone https://github.com/weldpua2008/ansible-nginx.git ~# cd ansible-nginx ~# docker r

我正在为ansible.Galaxy编写ansible剧本,它将在任何Linux发行版上运行。 但我觉得可以用Travis CI和Docker在包含Systemd的图像上进行测试。因为SystemD给出了一个错误:

无法获取D总线连接:未连接到service manager

如你所见 有错误

因此,我试图表示:

~# 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中启动服务的方式:


这是因为Docker(普通的“未授权”)容器没有运行
systemd
的权限(因为它想要访问
/sys/fs/cgroup
文件系统)

但这确实是因为Docker容器更喜欢运行一个进程,即您试图使用它们的进程——将它们视为“一个具有大量支持实用程序的Unix进程”,而不是“迷你VM”

您需要从Centos one继承一个简单的Dockerfile,它安装
nginx
,然后有一个最终的
CMD
,如下所示:

CMD /usr/sbin/nginx -g "daemon off;"

关于提示,这里是官方的
nginx
Dockerfile

我试图测试ansible playbook是否会将nginx安装在裸机服务器或虚拟机内部。我可以看出这就是您的repo的目的,编写和测试ansible脚本(看起来很好:),但TravisCI在Docker容器中运行测试的事实意味着你只能在Docker容器中运行。我在不同的Ubuntu版本上测试了Ansible,但在CentOs 7上它无法重新启动服务:是的,在阅读了这个问题后,问题似乎是Ansible在Docker容器中运行时没有正确检测到
systemd
。也许改变你在TravisCI上运行测试的方式会有所帮助?可能没有使用Docker?我只是测试在
特权模式下运行CentOS 7 Docker映像,看看是否可以修复它。在CentOS 6剧本中,nginx重启似乎失败,因为端口已经在使用()-可能nginx需要以不同的方式重启,或者已经在运行?
CMD /usr/sbin/nginx -g "daemon off;"