✍DOCKER-SSH CONFIGURATION WITH ANSIBLE :

Priya Soni
6 min readApr 15, 2021

HELLO EVERYONE✌✌,

In this article, I will demonstrate How we can configure webserver using ansible playbook.

And I will also show how to create our ansible playbook from the scratch . So , At the last we will Create An Ansible Playbook that will Retrieve New Container IP and Update the Inventory.

✍TASK DESCRIPTION :

⚡ Create An Ansible Playbook that will Retrieve New Container IP and Update the Inventory.

⚡So further Configuration of Webserver could be done inside that Container .

✍PREREQUISITE :

✅ Docker Installed.

For more clarity in docker setup you can visit my previous blog.

So, Here is the link of my blog which is given below :

Before doing any practical implementations It’s a good practice to create one separate directory to store all the files.

This directory is also known as our Workspace. In my system I created one workspace called “task14.2”. I am putting everything in this workspace and at the end of this article I will provide the GitHub link, from where you can download this workspace and also use it.

But before starting let’s understand some basic terms.

For basic of an ansible you can also visit my recent blog for more clarity.

Most of the time, Ansible uses the SSH protocol to go inside a Linux system for configuration management. Similarly, to go inside the docker container as well, Ansible needs an SSH connection with the container. Here, I will be using ‘CentOs’ as the base image and by default, SSH is not enabled inside this image.

For checking the ansible version :

# ansible — version

Go to our ip.txt file and ansible configuration file :

# cat ip.txt

# cat /etc/ansible/ansible.cfg

For checking the ansible hosts:

# ansible all — list-hosts

# ansible -m ping all

And hence, my first step would be creating an SSH enabled image.

I am going to use the above Dockerfile to create an SSH enabled image:

Here, I have created the image and to get the terminal or container using SSH, I have set the root password as ‘docker’.

To build the image:

To change the tag :

For pushing the repository in docker hub :

Finally, the SSH enabled image has been created:

Now, the next part is to create an Ansible playbook that will launch the container, retrieve its IP, and update the IP in the inventory dynamically.

Here is the Ansible playbook for the same:

# vim Container.yml

This playbook will ask for the container name, the exposed ports for SSH and Web Service and then it will launch the container from the given image (the same image we created in the above step).

After launching, it will register the output in a variable named ‘x’. Then it will retrieve the IP of the container for the variable and update it in a file called ‘ip.txt’

This ‘ip.txt’ file will be our inventory file and we have to pass this file in the configuration file of Ansible.

Here is an ‘ansible.cfg’ file I have created locally:

Now, let’s run this playbook:

# ansible-playbook — syntax-check Container.yml

Here is the output of the playbook:

# ansible-playbook Container.yml

NEXT :

Now , We can check the ip.txt file:

And as you can see, the inventory has been updated as well.

The next step is configuring something (let’s configure a webserver) by going inside the container and this thing will be done by Ansible only.

Here, is the Ansible playbook for the same.

# vim docker.yml

Now, let’s run this playbook:

# ansible-playbook — syntax-check Container.yml

Here is the output of the playbook:

# ansible-playbook docker.yml

So, Finally this playbook run successfully.

Let’s see port number is running or not :

For accessing the webpage:

Syntax : IP:Port (IP of the load balancer)

For eg.: 192.168.1.5:9005

To access webpage by using curl cmd :

Finally I have successfully completed my 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.

So guys, In the upcoming days I am going to be publish a lots of blogs and articles on different different automation tools and other technologies, So definetely follow me on Medium as well as on linkedin.

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

Here , I have also provide my docker hub profile link so you can also check my all the push images.

So, Here is my linkedin profile if you have any queries definitely comment below or DM me on linkedin.

I can see you in the next blog with some more amazing tasks.

THANK YOU🙏🙏 GUYS FOR READING MY BLOG…

SIGNING OF FORM MY SIDE 👋👋

KEEP LEARNING🙇‍♂️📖🙇….

KEEP SHARING✌✌….

--

--