Apache Httpd Webserver configuration on Docker using Ansible(by updating inventory with container’s IP)

Objective

Configuration of Apache Httpd Webserver on Docker container using Ansible by dynamic addition of container’s IP address to the inventory file.

Content of this Blog

  • Prerequisite
  • Project Understanding : Ansible Roles Setup
  • Output

Prerequisites

  • For Ansible, refer the following link:
  • For Docker, refer the following link:
  • For Apache Http, refer the following link:

Project Understanding : Ansible Roles Setup

Let’s understand each Ansible role implementation one by one

tasks : main.yml

Important Points

  • Mount directory is created along with repository for Yum and Docker.
  • Docker is installed and respective service are enabled.
  • Python 3.6 is installed, also Python SDK for Docker is installed as it is required for usage of Docker modules in Ansible.
  • SELinux state is changed to permissive to avoid hindrance in Docker container creation using Ansible modules. Also Firewall is disabled so web page to be set up in the next part could be accessed from outside as well.
  • Docker image are built using Dockerfile(mentioned below) and then pushed to DockerHub. Container from the same is created with SSH connectivity set up within it, also port 80 used for webserver is exposed using the concept of PAT(Port Address Translation).
  • Container’s IP address is retrieved and dynamically placed in the inventory file. Also EDCSA key is removed from known_hosts file present in Docker Host to avoid issues in accessing the new container generated via SSH for Ansible.

Note: In order to use “community.docker.docker_image” module, run the command mentioned below:

files : Dockerfile

Important Points

  • CentOS is used as base image and openssh-server is installed to set up SSH.
  • Password Authentication is enabled and mentioned in SSH configuration file.
  • Password value is mentioned i.e., “centos” and generates host keys using ssh-keygen.
  • sshd service is enabled and port 22 is exposed.

vars : main.yml

Important Points

  • Name of directory to be mounted is mentioned.
  • Username and password required to login to DockerHub so as to push the image created is mentioned.
  • Port number is mentioned for PAT(Port Address Translation) for the Apache Httpd webserver setup within the container.

templates : inventory.yml

Important Points

  • container_ip” variable is mentioned in the file using Jinja2 templating.
  • The value of the variable is obtained by retrieving the IP address of container created.

— — — — — — — — — — — — — — — — — — — — — — — — —

tasks : main.yml

Important Points

  • httpd is installed in the container accessed via SSH by the Ansible using the inventory file updated in the previous role after container is created.
  • Webpage to be accessed via webserver are copied from the files directory to the Document Root within the container.
  • httpd service is enabled only when the state of “Httpd Installation” changes.

files : index.html

Output

Usage of Virtual Machine’s IP Address to access the web page launched within Docker container
curl command used to access the content of web page using Docker container’s IP within Virtual Machine

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store