Task 14.2 and 14.3

Jyoti Pawar
5 min readJun 13, 2022

Hello connections !!

To get to know about ansible , webservers, etc. Kindly read my previous blogs on ansible:

Task 14.2: Ansible playbook that will retrieve new Container IP and update the inventory so that further Configuration of Webserver could be done inside that.

Step 1: Python3 should be installed on your controller node in order to install an ansible.

In my case, python3 is already installed. we can check its version by using the command:

python3 --version

Step 2: Now, let’s install ansible using the command:

pip3 install ansible

Step 3: Initially the inventory file is empty

vim /etc/ansible/ansible.cfg

Step 4: Ansible Playbook for container setup and auto-updating of the inventory file

vim docker.yml- hosts: localhost
vars_prompt:
- name: container
prompt: "Container Name"
private: no

tasks:
- name: Creating Docker Repository
yum_repository:
name: docker
baseurl: https://download.docker.com/linux/centos/7/x86_64/stable
gpgcheck: no
description: Docker Repo

- name: install docker
command: "docker"
register: d_s
failed_when: false

- name: Installing Docker
command: yum install docker-ce -y --nobest
when: d_s.rc != 0
args:
warn: no

- name: Starting Docker
service:
name: docker
state: started

- name: Installing file for ansible and docker integration
pip:
name: docker-py
state: present

- name: Pull httpd image
docker_image:
name: httpd
source: pull

- name: copy webpage
copy:
src: index.html
dest: /var/www/html/index.html

- name: Start httpd Container
docker_container:
name : "{{ container }}"
image: httpd
state: started
volumes: "/var/www/html/:/usr/local/apache2/htdocs/"
published_ports:
- 7000:81
register: docker_info

- debug:
var: docker_info.container.NetworkSettings.IPAddress

- name: "Updating in inventory"
template:
src: "nodeips.txt"
dest: "/root/main.txt"

Let’s check the index.html file

Step 5: Now let’s check the code is correct or not.

ansible-playbook --syntax-check docker.yml

Step 6: let’s Run the Playbooks

First, let’s set up the container and set up the inventory file

ansible-playbook docker.yml

Step 7: Inventory file is dynamically updated

Step 8: Now let’s see Docker Service started and enabled in the system

systemctl status docker

Here I have used some modules of Ansible which are as follows:

yum_repository ( For creating Docker Repo in Yum Configuration file in our Slave Node

Package: For installing the docker software),

service ( For starting the Service of Docker),

pip (To install the Docker Python),

docker_image ( For downloading the docker image from the Docker Hub),

copy (To copy my web page from Master (controller) to Slave(Target),

docker_container (For starting the docker webserver container).

Step 9: Let us see the output using the curl command

curl http://172.17.0.5/index.html

Step 9: Let’s use the docker ps command to see it is launch or not.

Hence, we have successfully completed the task.

Task 14.3: Create an Ansible Playbook which will dynamically load the variable file named same as OS_name and just by using the variable names we can Configure our target node.
(Note: No need to use when keyword here.)

STEP 1:-

Create a Folder of any Name in my case folder name is “webserver”.

STEP 2:-

Create file of any name in webserver folder.

STEP 3:-

Create ansible host file from any location:-

STEP 4:-

Configure your ansible configuration file that is /etc/ansible.cfg

💥NOTE:- Give Location of your Ansible host file :-

STEP 5:-

Content in /etc/ansible/hosts file.

STEP 6:-

Content in file os.yml

STEP 7:-

Create two more in folder webserver.

💥NOTE :- Both the name of file are same those i given above

STEP 8:-

Content of file RedHat.yml

STEP 9:-

Content in file Ubuntu.yml

STEP 10:-

Use Redhat and Ubuntu OS

This is my Redhat OS that is running in my local VM

STEP 11:-

Run the ansible playbook(os.yml) by using command “ansible-playbook os.yml”

STEP 12:-

Use Ubuntu Instance Public IP in browser

This shows Webserver in Ubuntu OS is Ready!!✅

STEP 13:-

Use RedHat vm IP in browser

Thanks for Reading the article !!! 🙌🏻

Hope you might find this article helpful!!!!

--

--

Jyoti Pawar

Devops || AWS || ML || Deep learning || Python || Flask || Ansible RH294 || OpenShift DO180