CONFIGURATION OF APACHE WEBSERVER IN DOCKER USING ANSIBLE
Goal:
🔰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
Before writing the play-book write down all the steps which we are going to perform using ansible
STEPS
1: Create a docker repository = https://download.docker.com/linux/centos/7/x86_64/stable
2: Download the docker package = docker-ce-18.09.1–3.el7.x86_64
3: Start and enable the docker services
4: Install docker SDK (pre-requisite for downloading image)
5: Pull the httpd image
6: Create directory = /ws
7: Copy html page = /ws/
8: Create container using httpd image (volume = /ws:/usr/local/apache2/htdocs
First check ansible is install or not successfully with the command
ansible --version
Create txt file in any directory and mention the ips of target or managed nodes , their users with passwords.
Check the target node ips by the command
ansible all --list-hosts
Now check the connectivity to the target node by pinging
ansible all -m ping
Create the ansible playbook with any name but the extension should be .yml and write the code given below
- hosts: all
tasks:- name: "Creating docker repository"
yum_repository:
name: Docker
description: Docker Repo
baseurl: https://download.docker.com/linux/centos/7/x86_64/stable
gpgcheck: no- name: "Downloading docker package"
package:
name: "docker-ce-18.09.1-3.el7.x86_64"
state: present- name: "Starting Docker Services"
service:
name: "docker"
state: started
enabled: yes- name: Installing Docker SDK
command: pip3 install docker- name: "Pulling httpd image from docker"
docker_image:
name: httpd:latest
source: pull- name: "Creating Directory"
file:
path: :/webpages
state: directory- name: "Copying html page"
copy:
src: "home.html"
dest: "/ws/"- name: "Creating container using httpd Image"
docker_container:
name: MyWebsite
image: httpd:latest
state: started
exposed_ports:
- "80"
ports:
- "8888:80"
volumes:
- /ws:/usr/local/apache2/htdocs
After writing the ansible playbook check the syntax by the command
ansible-playbook --syntax-check <file_name.yml>
If the syntax is right then run the playbook by the command
ansible-playbook <name.yml>
Now check in the target node docker is configured or not.
In the folder yum.repos.d/ , Docker.repo file is created
And check by the systemctl command the status of the docker
And the docker container of the httpd is also running
Check the website by curl command
curl 172.17.0.1/home.html
HELLO!!