🔰Ansible Playbook : Configuring Docker Container with HTTPD Server Image and Hosting a Webpage🔰

rishabhsharma
5 min readNov 27, 2020

Let’s understand few concepts related to our task :

What is Ansible?

Ansible is a radically simple IT automation engine that automates cloud provisioning, configuration management, application deployment, intra-service orchestration, and many other IT needs.

Being designed for multi-tier deployments since day one, Ansible models your IT infrastructure by describing how all of your systems inter-relate, rather than just managing one system at a time.

It uses no agents and no additional custom security infrastructure, so it’s easy to deploy — and most importantly, it uses a very simple language (YAML, in the form of Ansible Playbooks) that allow you to describe your automation jobs in a way that approaches plain English.

Advantages of Ansible

  • Free: Ansible is an open-source tool.
  • Very simple to set up and use: No special coding skills are necessary to use Ansible’s playbooks (more on playbooks later).
  • Powerful: Ansible lets you model even highly complex IT workflows.
  • Flexible: You can orchestrate the entire application environment no matter where it’s deployed. You can also customize it based on your needs.
  • Agentless: 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.
  • Efficient: Because you don’t need to install any extra software, there’s more room for application resources on your server.

Basic Ansible Architecture

Controller Node :

System where we install ansible, make Inventory, run ansible command and create playbooks and have ansible configuration file.

Managed/Target Nodes :

The network devices (and/or servers) you manage with Ansible. Managed nodes are also sometimes called “hosts”. Ansible is not installed on managed nodes that’s why they are agentless.

Inventory :

Ansible Inventory is a file which defines all the remote hosts (target nodes) i.e. the IP address, username, password, protocol etc.

Playbook :

In the ansible world the program files where we write ansible code are known as a playbook.

Ansible is used for Configuration Management and there are two ways :

  • Ad-Hoc Commands -Manual way of writing commands on CLI to automate a single task on one or more managed nodes.
  • Playbooks -Playbooks are the program files in ansible where we only once write code and use it as many times. These are written in standard YAML format helps to manage our information in key-pair value format.

Now it is good to go as we know some terms related to the task :

ARTH — Task 10 👨🏻‍💻

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

👉🏻Lets get started with the practical…😃

In my case I am having one Controller Node having IP 192.168.43.155

one Target/Managed Node having IP 192.168.43.26.

✔️Lets check the ansible version installed on my controller node :

# ansible --version

✔️Creating Inventory providing target node IP, username, password, & protocol:

# vim ip.txt

✔️ Providing Inventory to Configuration file of ansible :

# vim /etc/ansible/ansible.cfg

✔️ Now as we have provided inventory to our CN now we can check all the hosts of MN & their connectivity from CN by using a command :

# ansible all --list-hosts# ansible all -m ping

So we’re ready with our ansible setup for Configuring Docker Container with HTTPD Server Image and Hosting a Webpage:

✔️ Creating a Playbook :

# vim docker.yml

✔️ The command to execute the playbook :

# ansible-playbook docker.yml

✔️ Lets check all the operations we performed from our Managed Node :

1) Configured docker yum repo

2) Check Docker Service started and enabled

3) Pulled docker httpd image

4) Launched docker httpd container

The Docker container has been successfully launched with the name webserver in the managed node as mentioned in the above screenshot. But you can also see all the things after going inside the docker container. So follow all the command which are mentioned below :

5) Let’s check our container IP

6) Now the webserver has been deployed on the docker container with the help of the Ansible Automation tool. So Let’s check our webpage from the browser :

TASK COMPLETED👨🏻‍💻

Thanks for reading !!!😊✨

🔰Keep Learning ❗❗🔰Keep Sharing ❗❗

Sign up to discover human stories that deepen your understanding of the world.

rishabhsharma
rishabhsharma

Written by rishabhsharma

Data Engineer | Azure Databricks | AWS | PySpark | DevOps | Machine Learning 🧠 | Kubernetes ☸️ | SQL 🛢

No responses yet

Write a response