Ansible Playbook that configures docker and launches a web-server.

Abhijeet Bakale
4 min readApr 2, 2021

“In this blog we are going to see how we can configure a docker container with the help of ansible playbook and launch a web-server for us.This playbook launches a docker container, exposes it to public and copy a html code into it and launches a web-server using Httpd web-server.”

introduction to docker.

Docker is a software platform for building applications based small and lightweight execution environments that make shared use of the operating system kernel but otherwise run in isolation from one another. While containers as a concept have been around for some time, Docker, an open source project launched in 2013, helped popularize the technology, and has helped drive the trend towards containerization and microservices in software development that has come to be known as on containers development.

What is container technology?

A container is a standard unit of software that packages up code and all its dependencies so the application runs quickly and reliably from one computing environment to another. A Docker container image is a lightweight, standalone, executable package of software that includes everything needed to run an application: code, runtime, system tools, system libraries and settings.

Docker allows you to run containers locally, eliminating disparity between your development and production environments, and everything in between. There is no need to install software packages locally. Everything you need for your development environment can simply run on the Docker engine as containers.

Introduction to Ansible

Ansible is a software tool that provides simple but powerful automation for cross-platform computer support. It is primarily intended for IT professionals, who use it for application deployment, updates on workstations and servers, cloud provisioning, configuration management, intra-service orchestration, and nearly anything a systems administrator does on a weekly or daily basis. Ansible doesn’t depend on agent software and has no additional security infrastructure, so it’s easy to deploy.

Ansible Playbook

Ansible is a tool used for configuration management. In this context, configuration management entails keeping a record of company hardware and software, and making changes to them remotely.

Ansible Playbooks are the files where Ansible code is written. These files are written in the language, YAML, which is a funny acronym for, “YAML Ain’t no Markup Language.”

Play-books contain one or more Plays. Plays map a group of computers to a few well-defined roles known as
Tasks. Tasks can be defined as Ansible scripts.

This playbook does 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.

You can also directly download this code :

Lets Start :

Installed ansible through pip install ansible and created the host-file which contains all this info of slave node.

setting the Slave node configuration file so that our ansible system can connect to the targetnode.

Creating the ansible.cfg which is configuration file for ansible and storing their our inventory.

Created the inventory file and giving ansible the location of our hostfile.

Created a ansible playbook and in this playbook we had configured yum repository for docker and installed docker package and through pip we had installed the docker sdk, Also started the docker service.

In this part of playbook we had created a folder named webserver in docker container and copied our html code in that folder. Also we had started our httpd server and exposed our container through nodeport and port no 80 so that it has outside world connectivity.

After running our playbook we see that our docker service is installed and enabled.

The playbook has also launched the container successfully

Thankyou, Hope this might help you.