Configuring Docker Using Ansible

Configuring Docker Using Ansible

Let’s see how we can configure an Apache Web Server on the top of Docker in any managed node with help of the Redhat Automation tool Ansible.

Before configuring the Setup I want to introduce you to Ansible and Apache web server and docker so that you have a little bit of idea about these tools and you can easily understand why we are using the Redhat Automation tool to configure the webserver on the docker engine in the managed node and how Ansible will help to automate the configuration of the webserver in the docker. So without any delay let’s jump into the introduction part of both tools.

🤔 What is Ansible?

Ansible is an Automation 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.

🤩 While Ansible may be at the forefront of automation, systems administration, and DevOps, it's also useful to everyday users. Ansible allows you to configure not just one computer, but potentially a whole network of computers at once, and using it requires no programming skills. Instructions written for Ansible are human-readable. Whether you're entirely new to computers or an expert, Ansible files are easy to understand.

🤔 Why Ansible?

⚡ Automation is an essential and strategic component of modernization and digital transformation. Modern, dynamic environments need a new type of management solution that can improve speed, scale, and stability across the enterprise IT environment.

💠 No matter the complexity of your environment or where you are on your IT modernization journey, an IT operations automation strategy can help you improve existing processes. With automation, you can save time, increase quality, improve employee satisfaction, and reduce costs throughout your organization.

✨ Ansible Is...

1 ) Simple

⏩ Human readable automation

⏩ No special coding skills needed

⏩ Tasks executed in order

2 ) Powerful

⏩ App deployment

⏩ Configuration management

⏩ Workflow orchestration

3 ) Agentless

⏩ Agentless architecture

⏩ Uses OpenSSH and WinRM

⏩ No agents to exploit or update

🔴 Ansible Architecture

No alt text provided for this image

Ansible is a radically simple IT automation engine that automates cloud provisioning, configuration management, application deployment, intra-service orchestration, and many other IT needs.

Being designed for multi-tier deployments since day one, Ansible models your IT infrastructure by describing how all of your systems inter-relate, rather than just managing one system at a time.

It uses no agents and no additional custom security infrastructure, so it’s easy to deploy - and most importantly, it uses a very simple language (YAML, in the form of Ansible Playbooks) that allow you to describe your automation jobs in a way that approaches plain English.

Problem Statement

Write an Ansible PlayBook that does the following operations in the managed nodes:

  1. Configure Docker.
  2. Start and enable Docker services.
  3. Pull the httpd(Apache) server image from the Docker Hub.
  4. Run the docker container and expose it to the public.
  5. Copy the html code in /var/www/html directory and start the webserver.
Note: I am setting up all the environments on the AWS Redhat Instance but you can set up this at any operating system i.e virtual machine (VM) Bare metal as well as a container.

Hopefully, now you have clear what we are going to perform. So let’s see one by one all the steps and what are the pre-requirements to set up the webserver in the docker container using ansible.

Prerequisite

We have to need the following tools and services to set up the webserver.

  1. Operating System i.e AWS instance, Container, Bare metal, or VM.
  2. Redhat Ansible
  3. Docker
  4. Apache WebServer

Now All are set so let’s see how to install the ansible on the AWS Redhat Instance and the same approach will work on VM etc.

We will have to first install the Ansible:-

pip3 install ansible 
No alt text provided for this image

After running the above command you can check it is installed or not using the below command.

ansible --version
No alt text provided for this image
Note: You are noticing that the config filehas none in the above screenshot so we have to replace none with an inventory. Now let’s see how we can replace it.

Now Ansible has installed in the controller node but we will manage the managed node with the help of the controller node which has the Ansible tool’s power so we have to access the managed nodes to install or anything whatever you want. For whom we have to install sshpass package. But before install this package we have to install its rpm file. So, run the below-mentioned command for rpm of sshpass.

sudo dnf install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm -y

No alt text provided for this image

After installing rpm of sshpass you can install this package easily using the below command.

sudo dnf install sshpass -y
No alt text provided for this image

After it, we have also to upload the key pair of the managed node in the controller node because we are using the AWS instance and if you want to log in with the instance then you have private key pair of it. So, upload the key-pair of the managed node into the controller node using the below-mentioned command.

scp -i ./your_key_pair.pem -r ./keypair_directory/* ec2-user@pubic_ip_of_controller_node

After uploading the key pair, give permission for the key pair using the below command.

chmod 400 your_key_pair.pem

No alt text provided for this image

Now, we will create an inventory:

✅ Inventory

Inventory is lists of nodes or hosts having their IP addresses, databases, servers, etc. which are needed to be managed.

 So make a file for it using the below command.

vim ip.txt

After running vim ip.txt, write the public IP of the managed node as well as username also as mention in the below screenshot.

No alt text provided for this image

There are other 3 fields or variables that can be seen in this file

ansible_user — This defines the target node username. In AWS EC2 Instance, the by default username for each EC2 Instances (except Windows) is “ec2-user”.

ansible_ssh_pass — This defines the private key file which is used for authentication when needed (Because EC2 Instance doesn’t support direct passwords, we need to use Private Key File instead of Password to connect Target Node (EC2 Instance) for Ansible. I’ve used “key.pem” which is located at the “/home/ec2-user/” custom directory (the directory can be changed according to the user)

ansible_connection = This defines the connection type between Managed nodes and the control nodes. I’ve used “ssh” as the Connection type.

After saving the above file with the help of ESC then press :wq now check it out our managed node is configured not using the below command.

ansible all --list-hosts
No alt text provided for this image

But you can check out in the above screenshot no host is configured. Now we have to create an inventory for configuring the managed node. But before it, we have to make a /etc/ansible directory for the inventory and go inside the ansible.cfg file using the below-mentioned command.

sudo vim /etc/ansible/ansible.cfg
No alt text provided for this image

After opening the ansible.cfg file, write the code inside the directory which is mentioned in the screenshot and save it.

[defaults]
inventory=/home/ec2-user/ip.txt
host_key_checking= False
private_key_file=/home/ec2-user/ansibleCN.pem
ask_pass=false


[privilege_escalation]
become=true
become_method=sudo
become_user=root
become_ask-pass=false

No alt text provided for this image

Now the inventory is ready for use so let’s check it working or not using the below command.

ansible all --list-hosts
No alt text provided for this image

Now you can see in the above screenshot, the controller node has one managed node now. Also Now let’s check config file has any inventory or not using the ansible --version

No alt text provided for this image

Nowconfig file has/etc/ansible/ansible.cfg as mentioned in the above screenshot. Also, let’s check the managed node is pingable or not using the below command.

ansible all -m ping
No alt text provided for this image

The managed node is pingable now so the Ansible setup has configured on the AWS instance so we can go forward for the next part of this task.

✅ Playbooks

Playbooks consist of your written code, and they are written in YAML format, which describes the tasks and executes through the Ansible. Also, you can launch the tasks synchronously and asynchronously with playbooks. Playbooks contain the steps which the user wants to execute on a particular machine. And playbooks are run sequentially. Playbooks are the building blocks for all the use cases of Ansible.

⏩ Playbook Structure

Each playbook is a collection of one or more plays. Playbooks are structured by using Plays. There can be more than one play inside a playbook.

No alt text provided for this image

✅ Hosts

In the Ansible architecture, hosts are the node systems, which are automated by Ansible, and any machine such as RedHat, Linux, Windows, etc.

✅ Networking

Ansible is used to automate different networks, and it uses the simple, secure, and powerful agentless automation framework for IT operations and development. It uses a type of data model separated from the Ansible automation engine that spans the different hardware quite easily.

No alt text provided for this image

Deploying web server on Docker using a Playbook

So let’s see all the steps one by one how we can deploy a web server on the docker container through ansible.

Step 1: Creating a docker repository

Before installing the docker software we have to create a docker repo so that we can easily install it in the Redhat Linux especially rhel 8. Also, you can see in the below screenshot that the managed node has no docker repo. So let’s how we can create it through an ansible-playbook.

No alt text provided for this image

Make a docker.yml using the editor and write the below-mentioned code inside it.

No alt text provided for this image

After saving the above code, run the playbook using the below command.

ansible-playbbok -v docker.yml
No alt text provided for this image

Now you can see in the below screenshot docker repository has been created in the managed node.

No alt text provided for this image

Step 2: Installing Docker Software

Docker repo has been created so now we can go forward with the installation of docker.

So write the below code in the same playbook docker.yml

No alt text provided for this image

After saving code, run ansible-playbook docker.yml command again.

No alt text provided for this image

Now you can see docker software has been installed so let’s jump into step 3.

Step 3: Starting the Docker Service

Now if you want to use docker services then you have to start its services. So let’s see how we will write the code for it in the playbook.

Open docker.yml playbook and write the mentioned code inside it.

No alt text provided for this image

After saving code, run ansible-playbook docker.yml command again.

No alt text provided for this image

So the controller node is showing that the docker service has been started in the managed node as mentioned in the above screenshot. But let’s see it is started or not inside the managed node using the below-mentioned command.

systemctl status docker
No alt text provided for this image

Docker services have been successfully started in the managed node as mentioned in the above screenshot

Step 4: Pulling the Docker image

After starting the services now we need a docker image so for whom we have to write the code for it inside the docker.yml playbook. But let’s see the managed node has any docker image or not using the below command.

docker images
No alt text provided for this image

The managed node has no docker images as mentioned in the above screenshot. So open docker.yml playbook and write the mentioned code inside it.

No alt text provided for this image

After saving code, run ansible-playbook docker.yml command again.

No alt text provided for this image

So the controller node is showing that the docker image has been pulled in the managed node as mentioned in the above screenshot. But let’s see it is pulled or not in the managed node using the below-mentioned command.

docker images
No alt text provided for this image

httpd docker image has been successfully pulled from the docker hub in the managed node as mentioned in the above screenshot.

Step 5: Copying the Webpage in the managed node

This part totally depends on you how you want to create the webpage. So this is totally your desire. But I am making a very simple html file as you can see in the below screenshot.

No alt text provided for this image

Now you have to copy this content into the managed node. So I want to copy index.html file in the /home directory.

So open docker.yml playbook and write the mentioned code inside it. But you can decide src and dest location whatever you want to give.

No alt text provided for this image

After saving code, run ansible-playbook docker.yml command again.

No alt text provided for this image

So the controller node is showing that the index.html file has been copied in the managed node as mentioned in the above screenshot. But you can also check in the managed node as I have shown in the below screenshot.

No alt text provided for this image

ndex.html file has been successfully copied from the controller node to the managed node as mentioned in the above screenshot.

Step 6: Launching the Docker container for the webserver

Now we are at the last step and will see how we can launch a container for the webserver. But let’s check first that the managed node has any docker container or not using the below-mentioned command.

docker ps -a
No alt text provided for this image

The managed node has no docker container as mentioned in the above screenshot. So open docker.yml playbook and write the mentioned code inside it.

No alt text provided for this image

After saving code, run ansible-playbook docker.yml command again.

No alt text provided for this image

So the controller node is showing that the docker container has been launched for the webserver in the managed node as mentioned in the above screenshot. But let’s see it is launched or not using the below-mentioned command.

docker ps
No alt text provided for this image

The Docker container has been successfully launched with the name webserver in the managed node as mentioned in the above screenshot. But you can also see all the things after going inside the docker container. So follow all the command which are mentioned in the below screenshot

No alt text provided for this image

Now the webserver has been deployed on the docker container with the help of the Ansible Automation tool. So let’s check the webpage is responding or not.

No alt text provided for this image

Conclusion

Hopefully, now you have an idea what is the use of ansible here how we can deploy the webserver on the docker container through Ansible as well as how we can install the Ansible tool on the AWS Redhat instance. If you have any problem with the playbook then you check in the Github repo.

Congratulation …Now we have successfully completed everything.
Thanks!!!!
From:
Adnan A. Shaikh










hi Adnan Shaikh I have tried same as you guided and placed pem file as well in the folder. but receiving below error.

  • No alternative text description for this image
Like
Reply

To view or add a comment, sign in

More articles by Adnan Shaikh

  • AWS Cloud Front configuration using AWS CLI

    AWS Development Tools Amazon has empowered the developers and architects to develop applications on AWS in the…

  • The Magic of AWS CLI

    We have seen Hollywood movies and Web series where a person tries to hack a system using CLI. But what is CLI? It is…

    1 Comment
  • Multi-Cloud Computing

    What is Multi-Cloud Computing? Multi-cloud (also multicloud or multi cloud) is the use of multiple cloud computing and…

    1 Comment
  • Hosting Website using Amazon EFS

    This is the extended part of task 1 here we will use EFS instead of EBS Statement 1. Create a Security group that…

    2 Comments
  • AWS VPC Security

    Statement 1. Write an Infrastructure as code using Terraform, which automatically creates a VPC.

  • Virtual Private Cloud (VPC)

    What is VPC? Amazon Virtual Private Cloud (Amazon VPC) lets you provision a logically isolated section of the AWS Cloud…

    2 Comments
  • Amazon EKS Fully managed Kubernetes Services

    What is EKS? Amazon Elastic Kubernetes Service (Amazon EKS) is a managed service that makes it easy for you to run…

  • The Future of Computing

    The goal of cloud computing is to allow users to take benefit from all of these technologies, without the need for deep…

Others also viewed

Explore content categories