As shown in the diagram, we are trying to provision, docker container in remote host using a intermediate docker container (Docker-Ansible) which has been installed Ansible.
So Ansible playbook will run inside the docker container which is in local host.
By this way you don't have to install Ansible in local machine.
Also we can execute multiple playbook at the same time.
Please note, for this demonstration I am using single machine. You can use two virtual machines instead of single machine with different IP address.
Steps.
1. Install Open-SSH server in Ubuntu.
Please follow below article for this.
https://www.cyberciti.biz/faq/ubuntu-linux-install-openssh-server/
2. Setup SSH keys in Ubuntu.
Please follow below article for this
https://www.digitalocean.com/community/tutorials/how-to-set-up-ssh-keys-on-ubuntu-1604
https://www.ssh.com/ssh/copy-id
3. Install Docker
https://dhanuka84.blogspot.com/2019/02/install-docker-in-ubuntu.html
4. Install Python
https://tecadmin.net/install-python-2-7-on-ubuntu-and-linuxmint/
5. Clone below two github projects
git clone https://github.com/dhanuka84/docker-ansible.gitgit clone https://github.com/dhanuka84/docker-ansible-playbook.git
6. Build each project
cd docker-ansible/master-ubuntu16.04
docker-ansible/master-ubuntu16.04$ docker build -t ansible-docker:master-ubuntu16.04 .
cd docker-ansible-playbook
docker-ansible-playbook$ docker build -t docker-ansible-intermediate:latest .
7. Provisioning the container using intermediate container
cd docker-ansible-playbookdocker-ansible-playbook$ ./start.sh
Explanation
1. Docker image ansible-docker:master-ubuntu16.04
If you look at the Dockerfile, you can see it has setup up all the things mentioned in the diagram as Docker-Ansible. This will be the generic Ubuntu based image.
2. docker-ansible-intermediate:latest
We use previous image as parent image in here. Mainly what we do with this image is setup SSH keys and hosts
Though define ssh-keyscan 192.168.0.114 > /root/.ssh/known_hosts in the image level is not the best approach for this just for demo purpose we did like this.
Best approach is do the same in container level (start.sh) instead of image level.
3. docker-ansible-playbook$ vim docker.yml playbook
---- hosts: docker# gather_facts: notasks:- name: Create containerdocker_container:name: docker-test# docker_host: "tcp://localhost:22"# This is a pre-built ubuntu based image. Also it has been installed python-pip as explain in the #diagram.#We use this image as provisioned containerimage: nitincypher/docker-ubuntu-python-pip:latest# image: ansible:ubuntu16.04command: sleep 1ddetach: trueinteractive: truetty: true# tls_hostname: localhosttls_verify: yes
References:
https://developer.ibm.com/tutorials/cl-provision-docker-containers-ansible/