An ansible playbook to configure docker,start docker services and launching a webserver on the top of docker.

Sanat Dash
3 min readJun 17, 2021

Task 10.

Task description-

🔰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 docker container and expose it to the public
🔹 Copy the html code in /var/www/html directory
and start the web server

Note- Ansible should be installed on your system, and the nodes where this docker is to be configured should have internet connectivity.

Create the Docker Repository

  • To install docker, first we need to configure yum.

Install Docker

  • You can install Docker using “command” module or “package” module.
  • “Command” module directly runs the given command in hosts.
  • “Package” module will automatically

Install Python SDK then Docker SDK

  • To handle docker containers from ansible, Docker SDK is needed to be installed in managed nodes.
  • To install Docker SDK using pip module, first you need to install Python on hosts.
  • Install python using “package” module.
  • Then install docker SDK using “pip” module.

Start and Enable Docker

  • Start and enable the Docker using “service” module.

Pulling httpd image from Docker Hub

  • Pull “httpd” image from docker hub using “docker_image” module.

Launching Docker Container with httpd image and Exposing it

  • For launching container, use “docker_container” module
  • For “volumes” option, first we need to create the folder in host using “file” module, then mount this folder within the container.
  • Use “exposed_ports” option, to expose the container.
  • Use “ports” option to publish exposed container port to Docker host (Host_Port:Conatiner_Port)

Copy webpages to /var/www/html directory in Conatiner

  • Copy the webpage to the folder that is mount within the conatiner.

Setting Firewall Rule

  • Disable the firewall in the hosts so that client can connect to the webserver.
  • For disabling firewalld, you can either use “service” module or “firewalld” module.

Ansible playbook:-

Running Ansible Playbook-

Checking the node if it worked or not:-

Using Curl to check the webserver:

Now let check our webserver is working or not.

Finally the task is done.

Thanks for reading 😀

#python #vimaldaga #righteducation
#educationredefine #rightmentor
#worldrecordholder #linuxworld #arthbylw
#makingindiafutureready #righeudcation

--

--