Configure Docker using Ansible
By: Sanket Badjate
Tags: Ansible, Configuration, Docker, Paybook

I completed Task1 of ansible training given by World Record Holder Mr. Vimal Daga Sir at LinuxWorld Informatics Ltd Pvt.

Problem Statements

❗ Ansible Task 1❗

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

Additional task for DevOps AL and Hybrid Multi Cloud trainees:

🔹 Configure a Kubernetes Cluster having master and slave nodes

🔹 Do the above implementation as Deployment in the Kubernetes Cluster

Prerequisites

Ansible installed at Controller Node

Connectivity Between Controller Node and Managed Node

Managed Node must have the docker-py python module

Approach

I Used two VM of RHEL 8, one(IP 192.168.43.2 work as controller/manager node, ) another one (IP 192.168.43.239, managed node). I make this managed node as a group “web”.

I have done this task steps wise:-

Step 1

-> Inventory File (for managed node/ web group):-

Step2

-> Create Playbook:-



hosts: web // my managed node

vars_prompt: // create variable with take input from user

- name: dnsip

prompt: "Enter the your DNS ip "

private: no

tasks:

- name: copy the dns conf file // configure dns

template:

src: dns.conf

dest: /etc/resolve.conf

register: x

ignore_errors: yes



- name: add docker repo file // add a repo at managed node for docker

yum_repository:

name: docker

description: docker repo

baseurl:

gpgcheck: no

register: y

- name: status of add docker repo

debug:

var: y

- name: configure yum for docker, and install in webserver hosthosts: web // my managed nodevars_prompt: // create variable with take input from user- name: dnsipprompt: "Enter the your DNS ip "private: notasks:- name: copy the dns conf file // configure dnstemplate:src: dns.confdest: /etc/resolve.confregister: xignore_errors: yes- name: add docker repo file // add a repo at managed node for dockeryum_repository:name: dockerdescription: docker repobaseurl: https://download.docker.com/linux/centos/7/x86_64/stable/ gpgcheck: noregister: y- name: status of add docker repodebug:var: y - name: install docker software //install docker s/w

package:

name: "docker-ce-18.09.1-3.e17.x86_64"

state: present

register: z

when: x.failed == false

when: y.failed == false

- name: status of install docker software

debug:

var: z

- name: start the docker service //start the docker service

service:

name: "docker"

state: started

enabled: yes

register: a

when: z.failed == false





- name: copy the webpage from CN to webserver // copy webpage

copy:

src: "index.html"

dest: "/root/home/"

ignore_errors: yes

- pip: //python module

name: docker-py

- name: pull the image from docker hub // pull image

docker_image:

name: "aju121/myweb:latest"

source: pull

register: b

- name: status of pull the image

debug:

var: b

- name: launch the conatiner for hosting the webpage //create container

docker_container:

name: "ajayOS"

image: "aju121/myweb:latest"

state: started

ports:

- "88:80"

volumes:

- /root/home/:/var/www/html/



- name: Final Result

debug:

msg: "Successfully configured docker , launch container and hosting my webserver"

when: x.failed == false

Step3

Outputs:

-> Now I Have to run the playbook:-

ansible-playbook file_name

-> Now check the managed node:-

Note:- If you have queries related to this article please feel free to contact me.