INTEGRATING ANSIBLE AND DOCKER

Nischal Vooda
4 min readAug 4, 2020

--

Write an Ansible PlayBook that does the following operations in the managed nodes:

  • Configure Docker.
  • Start and enable Docker services.
  • Pull the httpd server image from the Docker Hub.
  • Run the httpd container and expose it to the public.
  • Copy the HTML code in /var/www/html directory and start the webserver.

Ansible:

Ansible is a tool that is used for automation. For example, if you have a heterogeneous environment for your company(systems with different O.S.). Some systems have RedHat Linux 6 and some have RedHat Linux 8. And you want to perform a specific task on both the O.S. Both Redhat 6 and Redhat 8 have different commands for certain things like starting the services. So, one option is to go to the different systems and run the commands manually but today is the world of Automation. Everything needs to be automatic and fast.

Ansible is one such tool that does this for you. You do not have to tell the commands or even the O.S you just have to tell it what it has to do and it explores itself which O.S. the required action has to be done and performs the operations accordingly. And the company may have a number of systems may be 1000’s (for example) so you cannot go to every system to do a specific task. You need to have some scripts that perform those tasks in a click. Ansible provides you with this capability. Docker helps you to launch your environment in just a matter of seconds. Integrating these two tools helps us achieve faster Automation.

Ansible playbook:

An Ansible playbook is an organized unit of scripts that defines work for a server configuration managed by the automation tool Ansible. Ansible is a configuration management tool that automates the configuration of multiple servers by the use of Ansible playbooks.

Pre-requisites:

  • 2 RHEL 8 O.S. CLI
  • ANSIBLE INSTALLED AND CONFIGURED

Configuration of Ansible:

In the /etc/myhosts.txt file we have created a group of the Managed Node Ip as a host in Controller Node. By this approach, we can control the automation that Node which is in the group. After the IP, give your username and password of that Managed Node.

Ansible works against multiple systems in your infrastructure at the same time. It does this by selecting portions of the systems listed in Ansible’s inventory file. You can specify a different inventory file using the -i <path> option on the command line. Not only is this inventory configurable, but you can also use multiple inventory files at the same time.

By using the following command you can get all the manage nodes

ansible all --list-hosts

to check connectivity we can use this command

ansible all -m ping

Now the setup of the Ansible is done here. So, we can go to the next step that is creating our own ansible-playbook. Ansible Support YAML language in the playbook. Hence I have created the docker.yml ansible-playbook.

Creating a yum repository for Docker:

Installing docker using package module:

Then, start and enable the docker service and install the python for the docker:

creating a directory in manage node:

copying home.html to manage node:

Creating a container using httpd image & expose the port:

our playbook has been created let's run it

to run the playbook use this command

ansible-playbook dockersetup.yml

output:

docker in manage node

Connect me on my LinkedIn as well.

--

--

No responses yet