Using Ansible to configure a webserver in a docker container

Durvesh Palkar
4 min readMar 21, 2021

In this article I’ll demonstrate you how to use Ansible to launch a webserver in a docker container. Refer my previous blogs on Ansible and docker to get more detailed information about the mentioned tools.

◼️ ANSIBLE INSTALLATION AND SETUP

Let’s first install ansible. As you know that ansible is an agentless tool i.e. we have to install ansible only on the Controller node. Ansible is built using python3, and hence ensure that you have python3 installed in your system.

To install ansible, use the following command: pip3 install ansible. Verify installation using ansible --version

Ansible internally uses a program called ssh pass, so we have to also install it. Use yum install sshpass command in RHEL8.

Ansible Controller Node (CN) connects to it’s Managed Nodes (MN) using one of the protocol viz. ssh, paramiko, rss, etc (for linux) and WinRM (for windows). However to tell CN about it’s MNs, you have to create an inventory file which includes some information like MN’s IP, authentication details, protocol to be used. Then we have to write the location of this inventory file in the ansible configuration file, so that ansible will use it.

inventory file: <MN-IP> ansible_user=<username> ansible_ssh_pass=<password> ansible_connection=ssh

Now you have to create a folder, and in which you have to create the ansible configuration file.

To list all the hosts (MNs) use ansible all --list-hosts. In my case I’m working with only one MN. You can add multiple hosts by adding their details in the inventory file.

To check connectivity with all the nodes, use ansible all -m ping

Now we are good to go. In this demonstration I’ll be creating an ansible playbook to configure a webserver on top of a docker container. It is advised (by my mentor) to first create a step-by-step chart of all the steps you would have performed manually to create this configuration setup, and then convert each step into an ansible task in the playbook.

✒ Steps to be followed:

  1. Since our managed node is a RHEL8 machine, for installing docker we have to first configure a yum repository for docker there.
  2. After setting up yum, we will Install docker in the MN.
  3. After successful installation of docker, start the docker service.
  4. We will need pip3 command in the futher step, therefore we have to install python first to get pip3 command.
  5. Install docker SDK for python using pip.
  6. Create a directory on the MN to store the webpage files.
  7. Copy the HTML code file of your webpage from CN to MN.
  8. Then pull the Apache httpd docker image in MN.
  9. And the last step is creating a container, mounting webpage in it and then exposing it.

After following the above steps, the final playbook will look like this:

I have created a sample HTML code for testing our webserver. Execute the above ansible playbook usingansible-playbook <filename>.yml . It will ask you to input the HTML file (along with the path), so that the file will be uploaded to the Managed Node by this playbook. After executing the playbook, it will show similar output.

After successful execution of the playbook, let’s verify by visiting the website

If you face any issue while connecting to this URL, the reason might be related to masquerading. You can test the above setup by just disabling the firewall.

THANKS FOR READING !!

--

--