简体   繁体   English

在docker中测试带有systemd服务的ansible playbook

[英]Testing ansible playbook with systemd services in docker

I guess it is not possible, respective useful, to conduct ansible-playbook tests in a docker instance, when they incorporate "service enabled" tests that rely in turn on a running init system, in that case of centos 7 this would be systemd. 我想在docker实例中进行ansible-playbook测试是不可能的,各自有用,当它们包含依赖于运行的init系统的“服务启用”测试时,在这种情况下,这将是系统化的。

To be clear: The tests aim to show that the ansible playbook is working correctly on a set of given OS instances, that it has to support, and, the ansible scripts will be deployed on bare metal / virtual machines. 需要明确的是:测试旨在表明ansible playbook在一组给定的OS实例上正常工作,它必须支持,并且ansible脚本将部署在裸机/虚拟机上。

So, for instance, testing this simple nginx yaml snippet incorporates a service: state: started declaration. 因此,例如,测试这个简单的nginx yaml片段包含一个service: state: started声明。

# ./ansible-nginx/tasks/install_nginx.yml

- name: NGINX | Installing NGINX repo rpm
  yum:
    name: http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

- name: NGINX | Installing NGINX
  yum:
    name: nginx
    state: latest

- name: NGINX | Starting NGINX
  service:
    name: nginx
    state: started

using the Dockerfile given: 使用Dockerfile给出:

$ cat Dockerfile
FROM ansible/centos7-ansible:stable

WORKDIR /provision

COPY hosts /etc/ansible/
COPY ansible-nginx /provision

CMD ["ansible-playbook", "deploy.yml"]

fails with the error here: 失败,错误在这里:

$ docker run -it foo

PLAY [localhost] ***************************************************************

TASK [setup] *******************************************************************
ok: [localhost]

TASK [NGINX | Installing NGINX repo rpm] ***************************************
changed: [localhost]

TASK [NGINX | Installing NGINX] ************************************************
changed: [localhost]

TASK [NGINX | Starting NGINX] **************************************************
fatal: [localhost]: FAILED! => {"changed": false, "failed": true, "msg": "no service or tool found for: nginx"}

NO MORE HOSTS LEFT *************************************************************
 [WARNING]: Could not create retry file 'deploy.retry'.         [Errno 2] No such file or directory: ''


PLAY RECAP *********************************************************************
localhost                  : ok=3    changed=2    unreachable=0    failed=1 

What would be a way to test ansible scripts that use systemd (because they will be executed on baremetal/vm) using docker on different OS versions? 在不同的操作系统版本上使用docker测试使用systemd的ansible脚本(因为它们将在baremetal / vm上执行)的方法是什么?

Actually I am testing Ansible playbooks quite a lot with a docker container as the target host - the trick is to divert the calls to SystemD's "systemctl" ... away to another script that does just do the hard work of start/stop services. 实际上我正在使用docker容器作为目标主机来测试Ansible剧本 - 诀窍是将调用转移到SystemD的“systemctl”......转移到另一个只执行启动/停止服务的脚本的脚本。 My docker-systemctl-replacement will inspect the *.service files around ... it can also work as the CMD init-process if you like to. 我的docker -systemctl-replacement将检查周围的* .service文件...如果你愿意,它也可以作为CMD init-process工作。

Based on this blog post http://developers.redhat.com this might work for test based on Docker: 基于此博客文章 http://developers.redhat.com,这可能适用于基于Docker的测试:

Dockerfile Dockerfile

FROM ansible/centos7-ansible:stable

RUN yum -y update; yum clean all
RUN yum -y install systemd; yum clean all; \
(cd /lib/systemd/system/sysinit.target.wants/; for i in *; do [ $i ==     systemd-tmpfiles-setup.service ] || rm -f $i; done); \
rm -f /lib/systemd/system/multi-user.target.wants/*;\
rm -f /etc/systemd/system/*.wants/*;\
rm -f /lib/systemd/system/local-fs.target.wants/*; \
rm -f /lib/systemd/system/sockets.target.wants/*udev*; \
rm -f /lib/systemd/system/sockets.target.wants/*initctl*; \
rm -f /lib/systemd/system/basic.target.wants/*;\
rm -f /lib/systemd/system/anaconda.target.wants/*;
VOLUME [ "/sys/fs/cgroup" ]
CMD ["/usr/sbin/init"]

docker run -d --name foo foo && sleep 10 && docker exec -ti foo ansible-playbook /provision/deploy.yml

I personally use Vagrant with Virtualbox a lot for testing, which is of course also an option. 我个人使用VagrantVirtualbox进行测试,这当然也是一种选择。 But in your case I would the (untested) Dockerfile above a chance. 但在你的情况下,我会(未经测试的)Dockerfile有机会。

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

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