Docker Automation using Ansible

Gupta Aditya
4 min readDec 18, 2020

So hey guys hope you all are doing today we are going to automate docker using ansible and launch our website using docker httpd image we are going to start from scratch completely new operating system yum configuration, docker installation, enabling services, launching containers all things in just line of commands excited!! to see so do I let's get started with practical.

For automation, we are going to use a Product called Ansible if you don't know what is Ansible, benefits of ansible then you can check my previous article to get insights into Ansible.

In ansible we have to make inventory that contains all IP for that you can make one text file at any location you want and inside that we have to give our target node IP, username, password, connection medium as shown below.

After making inventory we have to make a configuration file inside /etc/ansible with name ansible. cfg as shown below

Inside the configuration file, we have to give the inventory file path and some other parameters as shown below.

In the above picture you can see host_key_checking = False is used we might have connected to the remote system before using ansible via ssh and in that case, one key is saved in the .ssh folder to avoid the checking of that key we have used this parameter.

Once this is done install sshpass from yum install sshpass.

After this use Ansible all -m ping command to check connectivity with the remote system if all works well result will as shown below.

Firstly to configure yum in the operating system we are using the below playbook.

- hosts: all
tasks:
— ansible.posix.mount:
path: “/dvd”
src: “/dev/cdrom”
fstype: “iso9660”
state: mounted
— yum_repository:
name: “dvd1”
baseurl: “/dvd/AppStream”
description: “new”
gpgcheck: no
— yum_repository:
name: “dvd2”
baseurl: “/dvd/BaseOS”
description: “new base os”
gpgcheck: no
— yum_repository:
name: “Docker”
baseurl: “https://download.docker.com/linux/centos/7/x86_64/stable/
description: “Docker repo”
gpgcheck: no

Once the yum is configured next step is to download docker and launch our container.

You can find Playbook code in GitHub attach at the end :)

To download docker we have used command module to download python3 we use yum and to download python library we have used pip module to enabling docker services we use service module and to make directory we use file module and to copy file we have template module and launching container we have used docker_container module.

You might be thinking in ansible we don't have any requirement for target node so why we have downloaded python3 here ???

This is because Ansible is using python at the backend and some modules of ansible required some specific library at the target node to run in our case we require requests,docker-py.

Guys, here we come to the end of this blog I hope you all like it and found it informative. If have any query feel free to reach me :)

Github link:-https://github.com/guptaadi123/Ansible.git

Guys follow me for such amazing blogs and if have any review then please let me know I will keep those points in my mind next time while writing blogs. If want to read more such blog to know more about me here is my website link https://sites.google.com/view/adityvgupta/home.Guys Please do not hesitate to keep 👏👏👏👏👏 for it (An Open Secret: You can clap up to 50 times for a post, and the best part is, it wouldn’t cost you anything), also feel free to share it across. This really means a lot to me.

--

--