Ansible PlayBook To Configure WebServer on the top of Docker.

Yagyandatta Murmu
4 min readDec 2, 2020
automation

After reading this article you will be able to configure web server on the top of docker using ansible automation . In this blog I have explained how we can configure docker service with the help of ansible and how we can configure webserver on the top of docker with ansible.

Before we begin, let’s describe what Docker is. Docker is a set of virtualization tools that allows us to create, test, and deploy containerized applications quickly and easily on a dedicated server. It has become very popular and used almost everywhere in our daily lives. Thanks to containerization, we can quickly launch applications on different private cloud host platforms utilizing small bundles which contain all the needed packages, libraries and configuration file to run an application. These docker packages communicate via established network channels.

This blog includes:

📌Configuration of yum repository for docker.
📌Installation of docker.
📌Start and enable docker services.
📌Pull httpd server image from the Docker Hub.
📌Run docker container and expose it to the public
📌Start the web server on the top of docker.

Before we start configuring docker, we must ensure that usernames and passwords and connection type exist for each host in the file. Because 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.

Now, we are good to go for creating our own Playbook to run the code. Generally, Ansible Support YAML language in playbook. So I have created one playbook file as docker.yml and one html file as web.html to copy inside managed node. In this playbook, I have written all the task plays which will configure our requirement service.

To check the connectivity between control-node and managed node :

>> ansible all -m ping

1. Configuration of yum repository for docker :

I have added an external repository that will help us obtain the Docker software.

tasks:
- name: "configuring yum for docker"
yum_repository:
name: "docker"
description: "docker yum repo"
baseurl: "https://download.docker.com/linux/centos/7/x86_64/stable"
gpgcheck: no

2. Installation of docker in managed node with ansible :

To install docker with ansible we need an ansible module called package.

- name: "installing docker"
package:
name: "docker-ce-18.06.3.ce-3.el7.x86_64"
state: present

3. Start and enable docker service :

To start any service we use an module called service. With this module we can start the service and we can also enable the service permanently.

- name: "starting docker service"            
service:
name: "docker"
state: started
enabled: yes

4. Installation of Docker SDK :

Docker modules requires having the Docker SDK for Python installed on the host running Ansible. And to install with python we need python in our managed node .

#install python then install docker sdk- name: "installing python"
package:
name: "python36"
state: present
- name: "installing docker sdk"
pip:
name: "docker"
state: present

5.Pull the httpd docker image from Docker Hub :

To pull an image from docker hub we use an ansible docker module called docker_image .

- name: "pulling docker image"
docker_image:
name: "httpd:latest"
source: pull

5. Run docker container and expose it to the public :

To Launch a container here we are using an ansible module called docker_container . After Launching docker container we are doing port forwarding.

- name: "configuring web service on docker container"
docker_container:
name: "webserver"
image: "httpd:latest"
state: present
ports: "8080:80"
volumes: "/var/www/html:/usr/local/apache2/htdocs/"

6. Start the web server on the top of docker :

Here we are using apache web server (httpd). To copy the web file(html file), we need to create a html file in our control node.

#creating a web page
>> vim /root/ansible/web.html
#content is :
Hello There, Thank you for Visiting me!!

Install and configure apache web server :

- name: "copying html file to container"
copy:
src: "web.html"
dest: "/var/www/html/"

Starting docker container :

- name: "starting docker container"
docker_container:
name: "webserver"
state: started

Launched Webserver :

on web

Automating your infrastructure setup can not only save you time, but it also helps to ensure that your servers will follow a standard configuration that can be customized to your needs. With the distributed nature of modern applications and the need for consistency between different staging environments, automation like this has become a central component in many teams’ development processes.

code URL : https://github.com/yagyandatta/docker_with_ansible

Thanks for visiting me :)

--

--

Yagyandatta Murmu

Devops || MlOps || Flutter || Web Development || PYTHON || Data Science || AWS cloud || GCP || Azure