Webserver Configuration In Docker Using Ansible

Chaitanya
4 min readJan 5, 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

Here the system with ip 192.168.43.238 is a Controller Node and system with ip 192.168.43.116 is Managed Node.

Controller Node
Managed Node

What is Ansible?

  • Ansible is an open-source automation tool, or platform, used for IT tasks such as configuration management, application deployment, intraservice orchestration, and provisioning
  • Automation simplifies complex tasks, not just making developers’ jobs more manageable but allowing them to focus attention on other tasks that add value to an organization.
  • Ansible is an open-source tool. No special coding skills are necessary to use Ansible’s playbooks.
  • Ansible is agentless that you don’t need to install any other software or firewall ports on the client systems you want to automate. You also don’t have to set up a separate management structure.

Playbook:

  • Ansible playbooks are like instruction manuals for tasks. They are simple files written in YAML language (Human readable).
  • Playbooks are really at the heart of what makes Ansible so popular is because they describe the tasks to be done quickly and without the need for the user to know or remember any particular syntax.
  • Each playbook is composed of one or multiple plays, and the goal of a play is to map a group of hosts to well-defined roles, represented by tasks.

After installing Ansible (in controller node) from ‘pip install ansible’ we can check version of ansible through : ‘ansible — version’.

ansible 2.9.15

Inside the /etc/hosts.txt file we have to add IP address, userid and passsword of Managed node. (managed node systems are those where we want to configure the things).

Inventory:

The Ansible inventory file defines the hosts and groups of hosts upon which commands, modules, and tasks in a playbook operate. The file can be in one of many formats depending on your Ansible environment and plugins. The default location for the inventory file is /etc/ansible/hosts.

/etc/ansible/ansible.cfg

Playbook File:

ansible.yml is the playbook file:

  • Docker repo Configuration:
  • Docker Installation and starting services:
  • Creating a directory to copying the webpage:
  • Pulling Image , launching container and exposing it:

To run this playbook you need to write ansible-playbook playbookname.yml.

ansible-playbook ansible.yml

OUTPUT:

So, here we are done with the configuration in Managed node!!

IN Managed Node -

Checking docker install or not:

Docker installed successfully!

Here we can see new container OS WEBSERVER is also launched:

OS launched

Also we can see the IP address of new Container using:

IP of container: 172.17.0.2

So, we can see output of index.html using curl command:

webpage output

Thank you for reading!!

Hope u find this Interesting :)

--

--