AUTOMATING APACHE - WEBSERVER CONFIGURATION IN DOCKER 🐋 USING ANSIBLE
TASK- 1 [ANSIBLE]
- Configure Docker
- Start and enable Docker services
- Pull the httpd server image from the Docker Hub
- Run the httpd container and expose it to the public
- Copy the Html code in /var/www/html directory and start the webserver.
So, First, we have to create a Virtual Machine for Controller Node and Target Node. As you can see below VirtualBox figure I am using Ansible_Controller_Node as Controller Node whereas Ansible_Target_Node is Target Node.
Now, you can install ansible via pip command as it comes from python pip3 install ansible. after that, you can check the version of Ansible.
pip3 install ansible
Next, you can create a group of IP in (ip..txt) file, in that you have to write that Target Node IP as a host in Controller Node. Give your username and password of that Target Node. By creating, groups of IP known as Inventory can helps us to do automation that Node which is in the group. As you can see I have a single Target_Node IP. If you want to do automation/configuration on multiple Target Node then you can give their IP in one of the Group. You can give any name to the group.
Before running your playbook you need to check that all the Target Node is connected or pingable with the Controller Node or not. This can be checked by the following command. Those Managed Node IP is connected with Controller Node that only
Now, we are ready to go for creating our own Playbook to run the code in one-click. Generally, Ansible supports the YAML language in the playbook. So, I have created two files one is a playbook ansible.yml and the other is satish.html which we will be copied inside the container.
Also, you need to give the path of ip.txt (IP data) in this /etc/ansible/ansible.cfg file. To run this playbook we need to give ssh permission. This can be done via host_key_checking=FASLE. We can disable the command warnings by using the command:
command_warning=FALSE
Steps for Implementing the Ansible-Playbook YAML Code:
Let’s set up the Docker Repository-
- hosts: all
gather_facts: false
tasks:
- name: Creating Docker Repository
yum_repository:
name: docker
description: Docker Repo
file: /etc/yum.repos.d/docker
baseurl: https://download.docker.com/linux/centos/7/x86_64/stable
gpgcheck: no
enabled: true
Installing Docker:
- name: Docker Package Installation command: "yum install docker-ce --nobest -y"
Installing Python:
- name: Install Software Python36
package:
name: python36
state: present
Installing Docker Python Library-
- name: Install Docker Python Library
pip:
name: docker-py
Creating a WorkSpace on Target_Node
- name: Creating a WorkSpace on Target_Node:
file:
path: /root/TN_Ansible_WS
state: directory
Copying Content into Target Node:
- name: Copying Content Into Managed Node
copy:
src: "satish.html"
dest: "/root/TN_Ansible_WS"
ignore_errors: yes
Starting Docker Service-
- name: Starting Docker Service
service:
name: docker
state: started
Pulling httpd image from DockerHub-
- name: pull an image
docker_image:
name: httpd
source: pull
Creating a Docker Container and exposing it-
- name: Creating Docker Container
docker_container:
name: MyWebserverOS
image: httpd
state: started
detach: true
interactive: true
ports:
- "7777:80"
volumes:
- /root/AnsibleTask1WebWS/:/usr/local/apache2/htdocs/
Have a Look Once of Ansible-Playbook Codes of (ansible.yml)
Now we can run the ansible-playbook :
We have successfully run our Ansible Playbook code, now let’s take a look at our Target Node.
Look here, Docker is Running
Now Successfully, launched MyWebserver on top of the docker. Let’s access the web apache server using chrome browser.
- My Web Apache Server is Successfully Up & running on Docker.
Thank You Vimal Sir for giving me great knowledge.