Configuration of webserver on docker container using Ansible playbook

What is Ansible?

Ansible is a configuration management tool of operating system. It uses declarative language which means we just have to tell what we want to do rest everything is handled by Ansible tool. Ansible is the agentless tool, so we need to install ansible only in controller node. It’s appreciable feature is, it is idempotent in nature, if our desire is already present or executed in managed node then it will not run again. Python is the base language of Ansible. There are 2 types of nodes:-

  • Controller Node
  • Managed Node

Now we are going to 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

1. Ansible Installation Setup on Controller node:-

  • Install python3 :- Use command yum install python3
  • Install ansible:- Use command pip3 install ansible and then check the version by ansible --version
  • Install sshpass:- yum install sshpass

2. Steps on controller node:-

  1. Create inventory file:- <managed node ip> ansible_ssh_user=username ansible_ssh_pass=password

2. Create folder and create a configuration file inside it:-

3. Check the list hosts and ping them to check the connectivity:- Here I have connected only one machine. So it is showing only one ip in inventory.

4. Create a playbook :- vim <name of playbook>.yml

5. Run the playbook :- ansible-playbook <playboook_name>.yml

There are three color codes:-

  • Green :- Already Executed earlier
  • Orange:- Changed occur
  • Red:- Failed, there is an error.

6. After successfully running the playbook, check whether our webpage content is displayed. :- Use command curl <managed node ip>

Successfully launched a container and and configured webserver using ansible playbook!!!

Thanks for reading!