![](/img/trans.png)
[英]how to mount the nginx docker container into host using Ansible Playbook?
[英]Testing ansible playbook with systemd services in docker
我想在docker實例中進行ansible-playbook測試是不可能的,各自有用,當它們包含依賴於運行的init系統的“服務啟用”測試時,在這種情況下,這將是系統化的。
需要明確的是:測試旨在表明ansible playbook在一組給定的OS實例上正常工作,它必須支持,並且ansible腳本將部署在裸機/虛擬機上。
因此,例如,測試這個簡單的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
使用Dockerfile給出:
$ cat Dockerfile
FROM ansible/centos7-ansible:stable
WORKDIR /provision
COPY hosts /etc/ansible/
COPY ansible-nginx /provision
CMD ["ansible-playbook", "deploy.yml"]
失敗,錯誤在這里:
$ 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
在不同的操作系統版本上使用docker測試使用systemd的ansible腳本(因為它們將在baremetal / vm上執行)的方法是什么?
實際上我正在使用docker容器作為目標主機來測試Ansible劇本 - 訣竅是將調用轉移到SystemD的“systemctl”......轉移到另一個只執行啟動/停止服務的腳本的腳本。 我的docker -systemctl-replacement將檢查周圍的* .service文件...如果你願意,它也可以作為CMD init-process工作。
基於此博客文章 http://developers.redhat.com,這可能適用於基於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
我個人使用Vagrant
和Virtualbox
進行測試,這當然也是一種選擇。 但在你的情況下,我會(未經測試的)Dockerfile有機會。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.