Provision Docker container using Ansible

Posted By : Swarnim Kashyap | 29-Jun-2022

What are our requirements:

  • One Managed Node
  • One Target Node

WRITE AN ANSIBLE PLAYBOOK BY USING THE ABOVE STEPS :

  • Creates an inventory that includes the IP address of the Controller node and also updates the configure file of ansible-playbook.
  • vim /root/ip.txt

  • Do check ansible connectivity with nodes.
  • "ansible all -m ping"
  • Make a workspace in the managed node where we will write all ansible-playbook.

FINAL PLAYBOOK CODE


- hosts: all
tasks:
— file:
state: directory
path: “/dvd1”
— mount:
src: “/dev/cdrom”
path: “/dvd1”
state: mounted
fstype: “iso9660”
— yum_repository:
baseurl: “/dvd1/AppStream”
name: “mydvd1”
description: “my dvd for yum”
gpgcheck: no
— yum_repository:
baseurl: “/dvd1/BaseOS”
name: “mydvd2”
description: “my dvd for yum”
gpgcheck:
— name: Docker
yum_repository:
name: Docker
description: Docker repo
baseurl: https://download.docker.com/linux/centos/7/x86_64/stable
gpgcheck: no
— name: starting docker service
service:
name: “docker”
state: started
enabled: yes
— name: installing Docker SDK for pyhton3
command: “pip3 install docker-py”
— name: Pull a Docker image
docker_image:
name: httpd
tag: latest
source: pull
— package:
name: “httpd”
state: present
— copy:
dest: “/var/www/html/index.html”
content: “this is my webpage\n”
— service:
name: “httpd”
state: started
enabled: yes
— firewalld:
port: “80/tcp”
state: enabled
permanent: yes
immediate: yes

PLAYBOOK OUTPUT :


LET US SEE THE TARGET NODE :


As we have seen here ansible WebOS directory has been created, and also index.html page has been created in our target node

Let's test some docker commands in our target node:
Check the status of the docker by typing this mentioned command below.

"systemctl status docker"

"docker images" 
It will show the available docker images.

"docker ps" 
To see all the running containers we have launched.

YEAH THE APACHE WEB SERVER HAS BEEN SUCCESSFULLY CONFIGURED ?
Now checked the webpage by using this syntax on the browser?

http://[IP(TARGET NODE)]: PORT NO

The Apache web server successfully launched on the docker container!!

ThankYou

About Author

Author Image
Swarnim Kashyap

Swarnim is working as a DevOps Engineer. He is skilled in DevOps tools like Ansible, AWS,Docker, Linux and Jenkins. He believes in hard work and is always ready to face new challenges.

Request for Proposal

Name is required

Comment is required

Sending message..