Integration of Ansible with Docker

ivsntejesh
3 min readMar 2, 2021

An Ansible playbook that helps in the configuration of docker container

Hello, everyone !!!

Today I am going to discuss the use case of Ansible to perform the following tasks:

🔰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 docker container and expose it to the public
🔹 Copy the Html code in the “/var/www/html” directory and start the webserver

What is Ansible?

Ansible is an open-source software provisioning, configuration management, and application-deployment tool enabling infrastructure as code. It runs on many Unix-like systems and can configure both Unix-like systems as well as Microsoft Windows.

It helps in the Automation of IT processes. We write playbooks to automate the process. In playbooks, we create some tasks that need to be executed in the managed node.

Docker configuration:

Docker is a containerization tool. With the help of Docker, we can launch our containers (which are like OS) within seconds. Using these containers we can launch our applications.

First, we need to download the software of docker in the managed nodes.

This creates a repo for the docker container. Downloads docker software using the package module.

Start and enable Docker services

We use the service module of ansible to start the service in the managed node. It starts the service for docker.

Pull the “httpd” server image from the Docker Hub

Using the module called docker_image we can pull any docker image from the docker hub. We can also push images by writing push in place of pull.

Copy the Html code in the “/var/www/html” directory and start the webserver

Run the docker container and expose it to the public

This creates a docker container with the name “web-server”. We have mounted the “webws” directory to “htdocs” directory where all the HTML files are stored.

GitHub link: Task-10 link

To run the playbook run the command:

ansible-playbook task_10.yml

The playbook runs successfully. Now we need to check if we can access the webserver or not.

We can see the webserver is running successfully.

Thank you Vimal Daga sir for giving this task. Please do clap if you like the article.

Connect me on Linked-in for any issues and suggestions: Tejesh Linkedin.

--

--