Ansible: Configuring docker and setting up HTTPD server inside docker container using ansible playbook.

Yashraj Oswal
4 min readNov 28, 2020

Introduction to Ansible:

Ansible is an open-source tool, which is coded in python and is used to accomplish various IT tasks which involves configuration management, application deployment, setting up any software in remote operating system, and also managing system services simply using ansible playbook. Following is done through Controller Node, which we will discuss in our next section

Ansible Playbook:

Ansible playbook involves set of modules provided by Ansible and is written in either JSON or in YAML language. But i suggest to make use of YAML language as it makes code more readable and simple to understand. YAML is base on key value pair, where it requires a key and its value to perform as a ansible Playbook and manage remote system’s also known as target node.

Architecture / Working of Ansible:

Figure 1.a

In the above Figure 1.a, you can clearly see how ansible manage the target node via master node. Controller node is the one who centrally carries out managing tasks, and is responsible to make changes in the remote node also known as target node. Let us discuss it step by step….

Step 1: Configuring Ansible (Controller node)

  1. To configure Controller node, first install ansible

Command: pip3 install ansible

2. Now Let us create an IP database inventory file, you can create is any where eg: /root, and save it with .txt extension. IP database inventory file has details about the target-node which is used to reach the target-node and carry-out IT tasks. Generally Inventory file includes, ip address of target-node, Username, password of target-node and connection protocol SSH i.e Secure Shell Hashing is used, as our target node is Linux.So it compulsory requires,

  1. Ip adddress
  2. ansible_user
  3. ansible_ssh_pass
  4. ansible_connection

3. Next is to create and configuration file for ansible, which will help ansible to be aware of inventory file, we need to create a new directory named ansible :

mkdir /etc/ansible

Now in the ansible directory, create a new file ansible.cfg and add,

Explanation:

inventory variable: we have provided the ip database inventory file path

Now whenever we try to connect any ip 1st time using ssh, we need to get the key, so sometime it may show you some erroe, to avoid this make,

host_key_checking = False;

4. Now we are done with configuring our controller node, to check whether the ip database inventory and ansible.cfg file is appropriate configured,

Command: ansible all — list-hosts

To check whether target-node is active or not use,

Command: ansible all -m ping

If you get this message in green and get pong as reply, then your ansible controller node is successfully configured with Target-node .

Step 2: Design Ansible playbook:

GitHub Code link: Yashraj-oswal

Before running, let us check in our target-node, docker-ce package is peresent or not,

Now Running Ansible playbook:

Command: ansible-playbook docker-apache.yml

As you can see, playbook has successfully executed..!

Step 3: Check docker is installed, image is pulled or not, and current running container:

Command: systemctl status docker

Docker container, with name same as input is running in Target-node

Step 4: Check whether httpd server has setup or not:

Now first, we require ip_address of the docker container, for that use

Command: docker inspect httpd-server | grep IP

Now we will check it on browser, whether server is running or not,

So, Using Ansible playbook, Docker with httpd server is successfully configured at Target-node..!

Thank-you so much for reading..! Have a great day..! Keep Learning, Keep supporting.

--

--