๐€๐๐€๐‚๐‡๐„ ๐–๐„๐๐’๐„๐‘๐•๐„๐‘ ๐‚๐Ž๐๐…๐ˆ๐†๐”๐‘๐€๐“๐ˆ๐Ž๐ ๐ˆ๐ ๐ƒ๐Ž๐‚๐Š๐„๐‘ ๐”๐’๐ˆ๐๐† ๐€๐๐’๐ˆ๐๐‹๐„

Rohit Raut
3 min readNov 28, 2020

Ansible

About Ansible, Architecture, ๐‡๐จ๐ฐ ๐“๐จ ๐ˆ๐ง๐ฌ๐ญ๐š๐ฅ๐ฅ ๐š๐ง๐ ๐‚๐จ๐ง๐Ÿ๐ข๐ ๐ฎ๐ซ๐ž ๐€๐ง๐ฌ๐ข๐›๐ฅ๐ž explained in the below article.

Docker

About docker and manul process of installation of docker but in this article, we will automate it using Ansible.

The play-book is uploaded to the Github repository and a link is provided at the end of this article.

ANSIBLE PLAYBOOK AND AD HOC COMMANDS

ad hoc commands can run a single, simple task against a set of hosts in inventory hosts. The real power of ansible understands using playbook we have to configure infrastructure on multiple servers. The playbook is a text file written in YAML format and saved using yml extension.it is easy to read, write and understand.

The playbook can consist of multiple plays and each play is an ordered set of tasks run against hosts from the inventory file to reach the desired state of a managed node.

Description:

โญConfigure yum for docker.

โญ Install 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 and start the webserver.

Pre-requisite:

  1. Ansible should be installed in Node and this node is called a Controller Node. for installation refer the above article.

To see the version of ansible installed

ansible --version
`Ansible Version

List of Hosts in the inventory file.

Ansible Inventory

Ansible Configuration File

Ansible Configuration File

Letโ€™s Start

Configure Yum for docker

- hosts: all
tasks:
- name: "Configuring yum for docker software"
yum_repository:
file: "docker_repo"
baseurl: "https://download.docker.com/linux/centos/7/x86_64/stable/"
name: "docker"
gpgcheck: no
description: "docker repo"

Docker Installation

- name: "Docker Installation"
package:
name: "docker-ce-18.09.1-3.el7.x86_64"
state: present

Starting Docker Service

- name: "Starting Docker service"
service:
name: "docker"
state: started
enabled: yes

Installing Python3

- name: "Installing Python3"
package:
name: "python3"
state: present

Installing docker-py

To perform docker operation behind the scene Ansible uses docker SDK

To Install Docker SDK:

- name: "Installing docker SDK"
pip:
name: "docker-py"
state: present

Creating a WebPage Directory in managed Node

- name: "Creating directory In managed Node"
file:
path: "/root/webpages"
state: directory

Copy Webpages to Directory

- name: "Copy webpages to directory"
copy:
src: "/root/Ansible_playbooks/docker-webserver/webpages/index.html"
dest: "/root/webpages"

Pulling HTTPD Image From Docker Hub

- name: "Pulling HTTPD Docker Container Image"
docker_image:
name: "httpd"
source: pull
state: present

Adding rules to the firewall

- name: "Adding firewall rule"
firewalld:
immediate: yes
permanent: true
port: "8081/tcp"
state: enabled
- name: "Masquerade"
firewalld:
masquerade: enable
zone: public
state: enabled
permanent: yes

Restart Firewall

- name: "firewall restart"
service:
name: "firewalld"
state: restarted

Launching Docker container

- name: "Launch httpd container"
docker_container:
name: "Webserver"
image: "httpd"
ports:
- "8081:80"
state: started
volumes:
- /root/webpages:/usr/local/apache2/htdocs/

Final Output:

GitHub Link: https://github.com/rohitraut3366/Aache-Webserver-Configuration-in-docker-using-Ansible.git

Hope This will Help you all.

Thank you for reading!!๐Ÿ˜‡๐Ÿ˜‡

Connect with me On LinkedIn for further queries.

--

--