Ansible Task 1 (configuring Docker)
Task Overview
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 httpd container and expose it to the public
🔹 Copy the html code in /var/www/html directory and start the web server
Pre-requisites
- set up one manager node and one managed node
- install ansible in managed node by pip3 install ansible
- configure the ip managed node in a txt file and add that file to ansible config file at /etc/ansible/ansible.cfg
- check if all the managed nodes are pingable by
ansible all -m ping
File and their explation
Hosts File
- In this file we write all our ip addresses of managed nodes.
ansible_user: The user name to use when connecting to the host.
ansible_ssh_pass: managed node password for ssh connection. - file: myhosts.txt
location: /etc/myhosts.txt
[test]
192.168.43.89 ansible_user=root ansible_ssh_pass=redhat[cli]
192.168.43.117 ansible_user=root ansible_ssh_pass=redhat[gui]
192.168.43.101 ansible_user=root ansible_ssh_pass=redhat
Configuration file
- inventory: where all are host ips are stored
host_key_checking: it auto saves the ssh key for first time connection. - File: ansible.cfg
Location: /etc/ansible/ansible.cfg
[defaults]
inventory=/etc/myhosts.txt
host_key_checking = False
Main code File ( docker.yml )
- Step 1: we define on which set of hosts we want to run the ansible playbook.
- we have set a variable docker for docker full name with version which we will install on our manged node

- Step 2 : adding a yum repo of docker-ce and installing docker
Note: we have to write full name of docker with version for community edition. - Installing docker and printing std output

- Step 3: installing python so that we can use pip3 module from python.
Installing python module of docker from pip3. it is necessary for proper functioning of docker. - Starting the docker service permanently
- Stoping and disabling the firewalld of rhel
- copying our webapp code from managed node to managed node.

- Step 4 : pulling a docker image from docker hub
- launching a docker container named “httpd_webserver”
- mounting /var/www/html/ from host to container
- exposing port 81 for outside connectivity
- port forwarding port 81 -> 80
- finally printing full details of container just like (docker inspect httpd_webserver)

This is the final output
container is launched and and we can see our webpage.

While making the i faced some some errors of iptables so i m also sharing how i solved those errors.

Thank You