✍Configure Docker Using Ansible:

Priya Soni
6 min readJan 18, 2021

✍Task-description :

🔰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 /var/www/html directory and start the web server

✍What is Ansible?

Ansible is an open-source IT automation engine, which can remove drudgery from your work life, and will also dramatically improve the scalability, consistency, and reliability of your IT environment.

✍What is Ansible and what can it automate?

You can use Ansible to automate three types of tasks:

👉Provisioning: Set up the various servers you need in your infrastructure.

👉Configuration Management: Change the configuration of an application, OS, or device; start and stop services; install or update applications; implement a security policy; or perform a wide variety of other configuration tasks.

👉Application Deployment: Make DevOps easier by automating the deployment of internally developed applications to your production systems.

Ansible can automate IT environments whether they are hosted on traditional bare metal servers, virtualization platforms, or in the cloud. It can also automate the configuration of a wide range of systems and devices such as databases, storage devices, networks, firewalls, and many others.

The best part is that you don’t even need to know the commands used to accomplish a particular task. You just need to specify what state you want the system to be in and Ansible will take care of it.

✍Why we need Ansible?

There are many other IT automation tools available, including more mature ones like Puppet and Chef, so why would you choose Ansible? The main reason is simplicity.

Michael DeHaan, the creator of Ansible, already had a lot of experience with other configuration management tools when he decided to develop a new one.

He said that he wanted “a tool that you could not use for six months, come back to, and still remember.”

✍What is Docker and how does it work?

Docker provides the ability to package and run an application in a loosely isolated environment called a container. The isolation and security allow you to run many containers simultaneously on a given host. You can even run Docker containers within host machines that are actually virtual machines!

✍What is the purpose of Docker?

Docker is an open source containerization platform. Docker enables developers to package applications into containers — standardized executable components that combine application source code with all the operating system (OS) libraries and dependencies required to run the code in any environment.

✍Prerequisites:

In order to execute the automated setup provided by the playbook we’re discussing in this article, you’ll need:

👉One Ansible control node: an Red Hat 8 machine with Ansible installed and configured to connect to your Ansible hosts using SSH keys.

👉Make sure the control node has a regular user with sudo permissions and a firewall enabled.

👉One or more Ansible Hosts: one or more remote Red Hat 8 servers previously set up, these will be work as a managed node.

✍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. It is written in YAML language.

✍DOCKER AND IT’S CONTAINERS:

Docker is a set of platform as a service products that use OS-level virtualization to deliver software in packages called containers. Containers are isolated from one another and bundle their own software, libraries and configuration files; they can communicate with each other through well-defined channels.

✍How to configure Ansible ?

In this task , first we have to install Ansible using this command:

#pip3 install ansible

Now we have to check our ansible version using this command.

#ansible — version

Now we have to be check how many hosts we have right now.

So as you can see that there is no hosts in my managed node.

Now we need to make a file ip.txt and provide the IP of our managed node.

#vim ip.txt

Then we have to make changes in the ansible configuration file.

#vim /etc/ansible/ansible.cfg

Now make a new directory for the ansible playbook using this command.

#mkdir ansible-playbook

#cd /ansible-playbook

✍Writing the Ansible-playbook for docker:

We have to write our ansible playbook and also give the .yml extension which is complusory.

#vim task10.yml

After writing the code we will check the syntax of our code and it will not show any error if its correct.

#ansible-playbook — syntax-check task10.yml

Also we need to create an html file in our managed node which is as shown below:

#cat task10.html

Now I have to show my task10.yml code for your reference.

👉NEXT

👉NEXT

then finally we have to be run our Ansible-Playbook.

#ansible-playbook task10.yml

👉NEXT

Now we have to be go to our controller node and check docker is configure or not.

for this we have to be run following commands.

#docker ps

#cat /root/TASK10/task10.html

Then you can open the firefox and write the ip and port number in the format which is given below:

https://IP:PORT/file.html

and it will send you to the webpage you have made using ansible playbook.

I have also provide the link of my Github repository for this task which is given below.

Finally I have successfully completed this task. Thank you Vimal Daga sir for giving me such a great task. Sir your mentorship is a God gift for me to enhance my skills and I am very blessed because you are my mentor.

KEEP LEARNING🙇‍♂️🙇‍♂️….

KEEP SHARING✌✌….

--

--