Ansible integration with docker and launch apache web server

Backbencher05
3 min readJan 6, 2021

--

🔰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

Ansible

Ansible is an open-source software provisioning, configuration management, and application-deployment tool enabling infrastructure as code. It runs on many Unix-like systems, and can configure both Unix-like systems as well as Microsoft Windows

Unlike most configuration-management software, Ansible does not require a single controlling machine where orchestration begins. Ansible works against multiple systems in your infrastructure by selecting portions of Ansible’s inventory, stored as edit-able, version-able ASCII text files. Not only is this inventory configurable, but you can also use multiple inventory files at the same time and pull inventory from dynamic or cloud sources or different formats (YAML, INI, etc). Any machine with Ansible utilities installed can leverage a set of files/directories to orchestrate other nodes. The absence of a central-server requirement greatly simplifies disaster-recovery planning. Nodes are managed by this controlling machine — typically over SSH. The controlling machine describes the location of nodes through its inventory.Sensitive data can be stored in encrypted files using Ansible Vault.

Lets start setup part,

Step 1 : Configuration of docker

Let our target node is Rhel8 and to configure doker in rhel we need to first configure yum repository and install the software

Step-2: Start and enable Docker services

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

To pull the images from docker hub we need some requirements

  • python >= 2.6
  • docker-py >= 1.7.0
  • Docker API >= 1.20

Steps:

🔹 Run the docker container and expose it to the public
🔹 Copy the html code in /var/www/html directory
and start the web server

Now after complete playbook run the playbook

# ansible-playbook docker-cfg.yml

Now you can check we launch the complete docker container using ansible which is also expose to the port no. as mentioned in the playbook

code link:

https://github.com/Backbencher05/ansible-docker-apache.git

Thank you ,

feel free to ask any query

--

--