Using Ansible Playbook to configure docker and launch docker container using httpd image

Yukta chakravarty
2 min readDec 3, 2020

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

🔹 Start the web server

Prerequisites: python interpreter, Ansible, sshpass should be installed

run ansible — version

If config file = None ,then do the following:

mkdir /etc/ansible

vim /etc/ansible/ansible.cfg , press i to insert contents as below:

ansible.cfg file contents

Press Esc, then press :wq to save

In inventory give path to your inventory file or create inventory file as below:

Replace 192.168.2.8 with the IP of your managed node, redhat with your password managed node
Run ansible all — list-hosts

Step 1: Configure docker

  1. Configure yum repository for docker
  2. By default in yum the value of best is true so we get an error like no best match found and it will not download so we have to set its value to false , when using command line to download we add the nobest option to make value of best to false . By using ansible we can do this by changing the value of best=false in /etc/yum.conf
  3. Install docker

Step 2: Start and enable docker services

Step 3: Pull the httpd server image from the Docker Hub

  1. Install docker sdk
  2. Pull the httpd image

Step 4: Launch docker container using httpd image

Step 5: Start web server

As soon as we launch the container the web server is started

You can find the code at link below:

After running playbook you can check if it is working typing localhost:8080/webpage.html in browser of the managed node

To know more about httpd image refer link below:

--

--