Automate httpd webserver using Ansible and docker.

Anirudh Bambhania
4 min readJan 25, 2021

--

In this article i will show you how to 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

Docker is a set of platform as a service products that use OS-level virtualization to deliver software in packages called containers. Containers are isolated from one another and bundle their own software, libraries and configuration files; they can communicate with

each other through well-defined channels.

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.

Ansible Playbook β†’

  • To perform the above mentioned task we will create an ansible playbook on our managed node and run it. Here is the playbook that we are going to use

Explanation of playbook β†’

  • Docker configuration β†’ Docker does not come pre installed on the dvd so we have to download the community version from the internet, and to install any software in the rhel we have to use yum/dnf but for this we need to configure yum repository so that we can download and install the packages.
  • Service β†’ now start the docker services.
  • Docker also requires some dependencies so we need to install them to.
  • Create a directory in the target node, and the use of it we will see later.
  • Create a sample html webpage and copy it in the directory we have just now created.
  • Pull the docker image or in other words download and install the os image. Here we are going to pull the httpd server image from the docker hub.
  • Create a docker container with any name, image and then expose ports which we would like to expose to the internet.
  • Finally we have to mount or attach the directory that we have created earlier in which we have the webpage because in the documents of htttpd image it is mentioned to copy the pages to this path β€œ/usr/local/apache2/conf/httpd.conf”. Here is the link of the httpd image

https://hub.docker.com/_/httpd

  • Run the playbook
  • The playbook run successfully and we can also check it on the target node
  • As we can see that the docker container is also started in the target node and the image name is httpd that we have used in the playbook and the port no. 80 is also exposed this is also called PATing, is a sub concept of NAT, now if the client comes to the host ip i.e. the ip of the target node than it will be redirected to the docker container where our httpd service is running.
  • Output β†’ go to the browser and type the ip:port

β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€”

Thank You

--

--