Automation using Ansible, Configuring Docker

Snehalhingane
5 min readOct 8, 2020

I’ve configured Docker using Ansible by writing Ansible playbook. let’s see the task:

Task 1:

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 web server

What is Ansible?

Ansible is a configuration management tool. It helps in application deployment , task automation and more. We use ansible when you have some sequence of task to performed on different servers or operating system. In today’s world automation is necessary for quick deployment of application or any other things. We can automate chain of events by writing scripts. Some scripting uses iterative language approach in which we have to specify “What to do , and How to do?”. But ansible uses declarative approach where we just have to mention “What to do?” and How’s it gonna perform the tasks is none of our concern.

Where we install ansible that is known as a Controller Node , and we are going to use controller node to configure web servers and Docker on different nodes and that is known as Managed nodes.

What is Ansible Inventory?

  • Ansible works against multiple managed hosts in your infrastructure at the same time, using a list or group of lists is known as the inventory.
  • Once an inventory is defined, you use patterns to select the hosts or groups you want to run against to Ansible.
  • The default location for inventory is a file called /etc/ansible/hosts. You can also specify a different inventory file at the command line using the -i <path> option. You can pull the inventory file from dynamic or cloud sources or different formats (YAML, ini). Ansible has inventory plugins to make it flexible and customize.

What is Ansible playbook?

  • Playbooks are the files where Ansible code is written. … Playbooks are one of the core features of Ansible and tell Ansible what to execute. They are like a to-do list for Ansible that contains a list of tasks. Playbooks contain the steps which the user wants to execute on a particular machine.

What is ansible modules?

  • A module is a reusable, standalone script that Ansible runs on your behalf, either locally or remotely. Modules interact with your local machine, an API, or a remote system to perform specific tasks like changing a database password or spinning up a cloud instance.

Overview

I want to configure Docker , and for that first I have to configure yum and then install docker , start the docker services. To validate if everything works fine , We also have to configure web-server in my case my using apache.

As ansible is written on the top of python so we can use python package manager to install ansible.

pip3 install ansible

We have to write an ansible playbook for configuration of Docker.

For installation of docker , first we have to configure yum which allows us to automatically install or update packages.

Here I’ve mention in which host I would like to configure yum i.e rhelgui which has some IP Address. To mention this you have to make an inventory so that our controller node comes to know where you wanna perform the tasks.

Now we have to tell our yum where to collect all the docker packages from , so we do that by mentioning.

With the help of these two modules we can download and install docker in our managed nodes.

After installing docker , we have to start our docker services which we perform with the help of service module.

Now my ultimate goal is to build my own docker image and launch a container using that image which has apache web-sever configured.

So I’ve created a basic Dockerfile which fulfills the above criteria

It uses centos as a base OS and if my managed Nodes don’t have centos image , it automatically pulls the image from the docker repository. Also , I’ve installed httpd server and started its services. And for testing I’ve created a html webpage to see if everything works properly.

Copy module copies the Dockerfile and html file which I’ve created to the path mentioned and modules docker_image pulls the centos image that we mention in our dockerfile and perform sequence of commands which we specified.

Finally the last step is to launch a container where we have web -server configured.

docker_container module launches the container from the image webpage which we built using dockerfile. And we exposed it to port number 1234 for testing.

As we have written ansible playbook in yml format so we have to save the file with .yml extension.

There also might be a requirement of docker-py package.

To run ansible-playbook we use the command below:

ansible-playbook docker_config.yml

As we can see we got no red errors and everything seems to configured perfectly. Let’s test it by using the ip address and check if we get the webpage.

Tadddaaaa!!! it’s working!!!!

Left side image: Manage Node Right side image: Controller node

Thanks For Reading!!!!

--

--