Ansible Blog-1

Using Ansible to Configure Docker 🐳 and Apache Web Server

ANUPREET DUBE
Automation with RedHat Ansible
5 min readApr 26, 2021

--

💡Introduction to the Tools

🔰 Ansible is an open-source Tool by RedHat for Configuration Management of Remote Systems. It also works as a software provisioning and application-deployment tool and works on the concept of IaC (Infrastructure as Code). It runs on many Unix-like systems, and can configure both Unix-like systems as well as Microsoft Windows.

🐳Docker is a tool which can create Containers within a Second. These Containers can be used to create, deploy, and run applications thus allowing a developer to package up an application with all of the parts it needs, such as libraries and other dependencies, and deploy it as one package in the form of an Independent OS.

🔰 The Apache HTTP Server is a free and open-source cross-platform web server software. Its job is to establish a connection between a server and the browsers of website visitors (Clients) while delivering files back and forth between them.

📝 TASK

Write an Ansible PlayBook for configuring the Managed Nodes as follows :

  • Install and configure Docker Software in the MN
  • Start and Enable Docker Service
  • Pull the httpd server Docker image from the Docker Hub
  • Launch an httpd container in the MN and expose it to public world.
  • Copy the HTML code in /var/www/html directory and start the webserver in Container of MN.

📌Pre-Requisites

Configuration of Controller Node (CN) and Managed Node (MN). :

  • Ansible Installation
  • Inventory Configuration
Inventory Configuration
  • Connectivity established between MN and CN
  • Internet Access for both MN and CN
Internet Access for both MN and CN

🔑SOLUTION

1️⃣ Creating HTML Code for WebPage :

In the CN, create a simple HTML code. This code will be used to deploy the Web page in Webserver launched later.

HTML Code

2️⃣ Check that Docker is not installed in the MN yet

Login to MN, manually or vis SSH, and run the following Command

rpm -q docker-ce
MN docker-1
MN docker-1

This confirms that MN is a fresh clean Node.

3️⃣ Create ANSIBLE PLAYBOOK for MN configuration

  1. Creating yum repository for Installing Docker in MN :
- name: Creating Repository for Docker Installation
yum_repository:
name: docker_repo
description: Repo for Docker
baseurl: https://download.docker.com/linux/centos/7/x86_64/stable
gpgcheck: false

This will create a Yum Repo which contains URL to install stable version of Docker Software.

2. Installing Docker Software in MN:

- name: Installing Docker
package:
name: "docker-ce-18.09.1-3.el7.x86_64"
state: present

This code will Check for the docker software in MN, and if not present, then will install the Software.

3. Start Docker Service and make it permanent in MN :

- name: Start Docker service
service:
name: "docker"
state: started
enabled: yes

This will start the Docker Service in MN and Make it permanent.

4. Install Docker SDK

- name: Docker SDK
command: pip3 install docker

This will install Docker SDK via pip3, which is a python package installer.

5. Pull HTTPD Image from Docker Hub

- name: Pll HTTPD Image from Docker Hub
docker_image:
name: httpd
source: pull

The Docker Image for Apache Webserver will be downloaded from Docker Hub into the MN

6. Launch the WebServer Container and Copy the Web Page Code in Document Root of webserver

- name: copy
copy:
dest: "/var/www/html/"
src: index.html
- name: Launch Container from HTTPD Docker Image
docker_container:
name: httpd_webserver
state: started
exposed_ports: "80"
image: httpd
ports: 8080:80
volumes: /var/www/html/:/usr/local/apache2/htdocs
command: httpd -D FOREGROUND

The above Snippet will copy the webpage code from CN to the Document Root of Webserver.

This code will then be deployed as a webpage in the Container.

💯The Compiled Playbook will be as follows :

Playook-1
Playbook-2

🔆Executing the Playbook -

ansible-playbook   <playbook's Name>

The above command will execute the Ansible Playbook :

Each part of the playbook will be executed sequentially

Playbook Execution Logs
Playbook Execution Logs

Finally, The playbook is executed successfully.!!💯

To verify the success of Playbook, we can manually check the status of Managed Node :

✅The Docker software is installed, and the service is active and permanently enabled :

Docker Installed in MN

✅ The container containing WebServer is running and was launched from Httpd Image :

Container Launched

Check the IP of the Container by :

docker inspect <Container Name>

✅ In a new terminal, check the WebPage -

curl  http://<IP Of Container>:80/index.html
Web Page

The webpage is also running fine.!!!

So this is how Ansible can be used to set up Apache web server on top of Docker Container in a Remote System.

--

--