Using an ansible playbook to perform a few tasks on a managed node

Sakshi Sharma
5 min readJun 12, 2021

The main goal here is to write an ansible playbook that does the following tasks on 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 webserver

What is an Ansible playbook?

An ansible-playbook is basically a blueprint of certain tasks that need to be performed on the managed node. They are written in YAML format and use declarative language which means we only have to write “what” needs to be done instead of “how” it should be done. Hence we call it a blueprint.

To read more about the ansible-playbook in detail, you can refer to the following pages:

🔗https://docs.ansible.com/ansible/latest/user_guide/playbooks_intro.htm🔗https://www.simplilearn.com/what-is-ansible-playbook-article

Docker and Dockerfile:

Docker is open-source software that allows us to launch containers. These containers have a lightweight environment that is isolated from one another but share one operating system kernel.

There are two ways to create a docker image:

📌Using the “commit” command

📌Using a Dockerfile: Docker can build images automatically by reading the instructions from a Dockerfile.

What is a Dockerfile?

A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image. Dockerfile provides us with more features compared to the commit command.

For example, one can use Dockerfile when they need to create an image using automation.

The task mentioned above requires us to pull an httpd image from the docker hub. Instead of using a pre-created image, I decided to create an image using the Dockerfile which can be seen as follows.

Dockerfile
index.html file

Keywords of Dockerfile:

One significant part of a Dockerfile is the keywords that are used in it. Following are the keywords used in the above-mentioned Dockerfile:

FROM: It is used to specify the base image on which we want to build our image.

RUN: To run a specific Linux command which is implemented at the build time, we used this keyword.

COPY: We use this keyword when we want to copy files from the source to a destination folder in the image.

EXPOSE: When we want to expose a port inside the Docker image, we use this keyword.

ENTRYPOINT:

CMD:

Using the docker build command, the docker image is built. Here, we can also use “ — tag” instead of “-t”. Its purpose to give name and an optional tag to the image which is being built. All the things done in a docker container, like installing particular softwares, creating files, etc…forms different layers when their image is created. When we run a dockerfile all the layers, from the steps written in dockerfile are created. Docker build command always maintains those layers in the cache. If we make any changes in the dockerfile, and then we run it again, only the new steps are executed which saves time..

The “docker push” command is used to push the newly created image in the Docker hub registry.

Note: Before using the “docker push” command one needs to use the “docker login” command to login to the docker hub registry from the terminal.

As you can see, the image “sakshi2199/webimage” has been successfully pushed into the registry.

Ansible-playbook:

The following modules have been used in the playbook:

yum_repository module: This module can be used to add or remove yum repositories in the Linux operating system.

command module: Used to execute commands in the managed node. Unlike other modules in ansible, this is not idempotent.

service module: It is used to control different services on the managed node. It can help in starting or stopping the services.

yum module: “Installs, upgrade, downgrades, removes, and lists packages and groups with the yum package manager.” — Official ansible documentation

pip module: This module is used to manage the python library dependencies (i.e. one can install python libraries using this module).

docker_image module: This module is used to pull an image from the docker hub registry.

docker_container module: Used to manage the life cycle of a container.

Ansible-playbook output:

Before running the ansible playbook as you can see from the following screenshot that no containers are present in the managed node.

After running the playbook, a new container has been launched.

Note: I used the following command to find the IP of the launched container

“docker inspect container_id | grep IP”

Thank you so much for reading this article! Hope you enjoyed it!

--

--