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

Prerequisites

Project Understanding : Ansible Roles Setup

Let’s understand each Ansible role implementation one by one

Part 1: Ansible Role (container_creation)

tasks : main.yml

---
# tasks file for container_creation
- name: Mount Directory Creation
file:
state: directory
path: "{{ mount_directory }}"
- name: Mount Directory to CDROM
mount:
src: "/dev/cdrom"
path: "{{ mount_directory }}"
state: mounted
fstype: "iso9660"
- name: Yum Repository - AppStream
yum_repository:
baseurl: "{{ mount_directory }}/AppStream"
name: "dvd1"
description: "Yum Repository - AppStream"
enabled: true
gpgcheck: no
- name: Yum Repository - BaseOS
yum_repository:
baseurl: "{{ mount_directory }}/BaseOS"
name: "dvd2"
description: "Yum Repository - BaseOS"
enabled: true
gpgcheck: no
- name: Docker Repository Setup
yum_repository:
baseurl:
https://download.docker.com/linux/centos/7/x86_64/stable/
name: "docker"
description: "Docker Repository"
enabled: true
gpgcheck: no
- name: Check whether Docker is installed or not
command: "rpm -q docker-ce"
register: docker_check
- name: Docker Installation
command: "yum install docker-ce --nobest -y"
when: '"is not installed" in docker_check.stdout'
- name: Starting Docker Service
service:
name: "docker"
state: started
enabled: yes
- name: Python3 Installation
package:
name: "python36"
state: present
- name: Installation of Docker SDK for Python
pip:
name: "docker-py"
- name: Changing SELinux state to permissive
selinux:
policy: targeted
state: permissive
- name: Stopping Firewall
service:
name: "firewalld"
state: stopped
- name: DockerHub Login
docker_login:
username: "{{ dockerhub_username }}"
password: "{{ dockerhub_password }}"
- name: Build Directory Creation
file:
path: /root/dockerfile
state: directory
- name: Copying Dockerfile to the Build Directory
copy:
src: files/Dockerfile
dest: /root/dockerfile/Dockerfile
- name: Building Image from Dockerfile and pushing it to DockerHub
community.docker.docker_image:
build:
path: /root/dockerfile
name: "{{ dockerhub_username }}/centos_ssh"
tag: v1
push: yes
source: build
- name: Creation of container from the image created
docker_container:
name: centos_ssh_container
image: "{{ dockerhub_username }}/centos_ssh:v1"
pull: yes
detach: yes
state: started
ports:
- "{{ port_number }}:80"
register: container_info
- name: Retrieving Container IP
shell: echo '{{container_info["ansible_facts"]["docker_container"]["NetworkSettings"]["IPAddress"] }}'
register: container_ip
- name: Adding Container IP to the inventory file
template:
src: templates/inventory.yml
dest: /etc/inventory.yml
- name: Removing EDCSA key from known_hosts file
lineinfile:
path: /root/.ssh/known_hosts
state: absent
regexp: "ecdsa-sha2-nistp256"

Important Points

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

ansible-galaxy collection install community.docker

files : Dockerfile

FROM centos:latest
RUN yum install openssh-server -y
RUN echo "PasswordAuthentication yes" >> /etc/ssh/sshd_config
RUN echo "root:centos" | chpasswd
RUN ssh-keygen -A
CMD ["/usr/sbin/sshd","-D"]
EXPOSE 22

Important Points

vars : main.yml

---
# vars file for container_creation
mount_directory: "/dvd1"
dockerhub_username: XXXX
dockerhub_password: XXXX
port_number: "80"

Important Points

templates : inventory.yml

[container]
{{ container_ip.stdout }} ansible_ssh_user=root ansible_ssh_pass=centos

Important Points

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

Part 2: Ansible Role (container_webserver_setup)

tasks : main.yml

---
# tasks file for container_webserver_setup
- name: Httpd Installation
package:
name: "httpd"
state: present
register: httpd_install_status
- name: Web Page Setup
copy:
dest: "/var/www/html/index.html"
src: "files/index.html"
- name: Starting Httpd Service
command: "/usr/sbin/httpd"
changed_when: httpd_install_status.changed == true

Important Points

files : index.html

<b>Hello from Container</b>

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

DevOps & Cloud Computing Enthusiast