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.
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.
So, for instance, testing this simple nginx yaml snippet incorporates a service: state: started
declaration.
# ./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:
$ 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?
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. My docker-systemctl-replacement will inspect the *.service files around ... it can also work as the CMD init-process if you like to.
Based on this blog post http://developers.redhat.com this might work for test based on Docker:
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. But in your case I would the (untested) Dockerfile above a chance.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.