Ansible-Playbook for configuring Docker Webserver
By: Neeraj Singh Negi
Tags: Ansible, Ansible Playbook, Docker, Containers, Web Server

Ansible + Docker + Webserver

Configure the Inventory for Managed Node IP in the host file i.e hosts with credentials (username & password).

How did ansible know where to do the task? Mention the Inventory hosts file in the ansible configuration file i.e ansible.config.

Here the Controller Node established a network with Managed Node.

- hosts: 192.168.43.109

Now we are ready to do tasks in the Managed Node.

Step 1 Download docker software

Before we go for downloading the docker software. We have to configure the yum repository through this we can easily download it otherwise it will fail to download.

tasks:

- name: Adding repository

yum_repository:

name: "docker-ce"

description: "docker download"



baseurl:

"https://download.docker.com/linux/centos/7/x86_64/stable/" gpgcheck: no

enabled: yes

Download the docker software with the command module. We can also use the ansible package module but the Ansible package module doesn’t support any option

— nobest

We can rectify the error by going to the Managed Node and manually run the command. As we already know that Ansible behind use Yum or Dnf to download the packages.

yum install docker-ce

- name: "installing docker-ce..."

command: "yum install docker-ce --nobest -y"

Step 2 Start and enable Docker services

- name: "starting the docker service..."

service:

name: "docker"

state: restarted

Step 3 Pull the httpd server image from the Docker Hub

While pulling the docker image from the Docker Hub that says to download the docker.py library before pulling the image.

After we also need pip3 that comes from python36 to download the docker.py.

Download python3 and docker.py library.

- name: "Downloading python3"

package:

name:

- python36 - name: "Pip installing docker-py dependencies"

pip:

name: "docker-py"

Now, we can download a docker image.

- name: "downloading docker image...httpd"

docker_image:

name: "httpd"

source: pull

Step 4 Copy the html code in document root i.e /usr/local/apache2/htdocs/ directory and start the web server

Before copying the content of the webserver create a folder for the permanent volume that we can mount to another docker container also.

- name: “creating directory for docker…”

file: path: /root/ansible-docker

state: directory

Now copy the content of the webserver.

— name: “copying webserver content…”

copy:

content: “It works…”

dest: /root/ansible-docker/index.html

Step 5 Run the docker container and expose it to the public

Before exposing for simple and easy setup is to disabled the SELinux because SELinux won’t allow port numbers to use.

name: Disable SELinux

selinux:

state: disabled

Launching and Exposing docker container i.e web1 in the 8012 port. Also, the volume from the base OS mounted to the web1 is permanent.

- name: "launching docker container..."

docker_container:

name: "web1"

image: "httpd"

ports: 8012:80

state: started

volumes:

- /root/ansible-docker/:/usr/local/apache2/htdocs/

Note: You might get face some error while exposing docker container

While exposing weberver (httpd) you might get this error. Sol. Webserver Httpd image is work on the 80 port i.e is pre-defined.

While downloading the docker-ce software. Sol. Attach and mount your Redhat 8 dvd before going to download the docker-ce software.

Now run ansible-playbook.

ansible-playbook webserver-docker.yml

Hurry! Output comes 😍

Click to get code… Github