Automation in Ansible

Prince Raj
3 min readNov 24, 2020

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 httpd container and expose it to the public.
  • Copy the html code in /var/www/html directory and start the web server.

Pre-requisites :

  • There must be at least two virtual machines running, one is controller node and the other is managed node.
  • There must be Ansible installed in the controller node.

We can check the whether Ansible is installed or not its and its version.

Now let’s begin to solve our task.

Step 1 :

Configuring Ansible so that we can connect to the managed node from the controller node. For that, we created myhosts.txt file in /etc and written the IP address of the managed node which is called inventory and user_name and user_password of that managed node

Now configuring the ansible configuration file, so that managed node gets connected to the controller node.

Now to check the connectivity we need to ping and see whether both nodes are connected or not.

Step 2 :

Now I have created a web.yml file which is called playbook.And in my playbook I will write all the codes(which is called play)for automation required in our task.

For adding Docker repository for YUM we need the following commands.

Step 3 :

Now I have to Install docker and start its services. So, for that, we need the following commands.

Step 4 :

Here, I created a new directory in the managed node and copy the html file which I have already created to the directory. And finally installing Docker via pip .

Step 5 :

Here, I created a Docker container and exposed the port:80 and apply the PAT concept so that the managed node’s port:1234 can be used to access the webpage we created on the server.

And also mount the directory: /code created in the managed node to the directory : /usr/local/apache2/htdocs so that webserver can host the webpage we created.

Step 6 :

Now we are all set to run the playbook we created.

cmd: ansible-playbook web.yml

We can see from the above image that everything worked fine.

Outcome :

So, I have finally completed the task. THANK YOU.

WRITTEN By

Prince Raj

--

--