Automatic Configuration of Docker and Web Server by Ansible

Srasthy Chaudhary
4 min readAug 16, 2021

Before configuring Docker and WebServer by Ansible, let us understand some basics of Ansible and Docker.

Ansible

Ansible is an open-source automation tool, or platform, used for IT tasks such as configuration management, application deployment, intraservice orchestration, and provisioning. In simple words, Ansible is a configuration management tool based on declarative language.

Terminologies related to Ansible:

Control Node: Any machine with Ansible installed. You can run Ansible commands and playbooks by invoking the ansible or ansible-playbook command from any control node.

Managed Node: The network devices (and/or servers) you manage with Ansible. Managed nodes are also sometimes called hosts. Ansible is not installed on managed nodes.

Inventory: A list of managed nodes. Your inventory can specify information like the IP address for each managed node.

Modules: The units of code Ansible executes. You can invoke a single module with a task, or invoke several different modules in a playbook.

Playbook: Ordered lists of tasks, saved so you can run those tasks in that order repeatedly. Playbooks can include variables as well as tasks. Playbooks are written in YAML and are easy to read, write, share and understand.

Docker

Docker is a set of the platform as service products that use OS-level virtualization to deliver software in packages called containers. Containers are isolated from one another and bundle their own software, libraries, and configuration files; they can communicate with each other through well-defined channels.

Prerequisite:

  • Yum should be configured in the Managed Node.

This blog contains the following tasks:

Configure Docker

Start and enable Docker services

Pull the httpd server image from the Docker Hub

Run the httpd container and expose it to the public

Copy the html code in /var/www/html directory and start the
Webserver

  • Install Ansible in Controler Node using the command: pip3 install ansible
  • Install openssh using the command: yum install openssh

Here redhat is my Control Node and RHEL_arth is Managed Node.

  • Create an Inventory with any name, ex — ip.txt.
  • Write managed node username, Password and connection type in the inventory.
  • Check the connectivity with Managed Node using the command ansible all -m ping

Connectivity is checked by the message ping pong.

  • Create a directory mkdir: /etc/ansible
  • Inside etc/ansible create an ansible configuration file: ansible.cfg
  • Add the path of an inventory in configuration file and set host key checking false so that it will not verify when we first time connect through ssh.
  • Create Ansible Playbook: docker.yml
  • Run the playbook using the command: ansible-playbook docker.yml
  • Check the changes in Managed node

Earlier in Manage Node, docker was not installed but after running playbook docker is installed.

  • Docker repo created in /etc/yum.repos.d
  • httpd image launched in Docker
  • Check IP of the container

Webserver is launched

Thank You!

--

--