DEV Community

Cover image for Apache WebServer Configuration in Docker using Ansible
Nitesh Thapliyal
Nitesh Thapliyal

Posted on

Apache WebServer Configuration in Docker using Ansible

In this blog, we will configure Docker and configure webserver in docker container using Ansible.

Before we start, we should know about Ansible and Docker.

What is Ansible?

Alt Text

Ansible is an open-source automation tool, or platform, used for IT tasks such as configuration management, application deployment, intraservice orchestration, and provisioning. Automation is crucial these days, with IT environments that are too complex and often need to scale too quickly for system administrators and developers to keep up if they had to do everything manually. Automation simplifies complex tasks, not just making developers’ jobs more manageable but allowing them to focus attention on other tasks that add value to an organization. In other words, it frees up time and increases efficiency.

What is Docker?

Alt Text
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.

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. You can use any computer that has a Python installation as a control node - laptops, shared desktops, and servers can all run Ansible. However, you cannot use a Windows machine as a control node. You can have multiple control nodes.

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. An inventory file is also sometimes called a hostfile. Your inventory can specify information like the IP address for each managed node. An inventory can also organize managed nodes, creating and nesting groups for easier scaling. To learn more about inventory, see the Working with Inventory section.

Modules:

The units of code Ansible executes. Each module has a particular use, from administering users on a specific type of database to managing VLAN interfaces on a specific type of network device. You can invoke a single module with a task, or invoke several different modules in a playbook. Starting in Ansible 2.10, modules are grouped in collections.

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. To learn more about playbooks, see Intro to playbooks.

Let's get started:-

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

  • The system that you want to make Control Node install Ansible in it using command pip3 install ansible, then install openssh using command yum install openssh.

Alt Text

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

  • After installing Ansible create an Inventory anywhere in the system, here I have created an inventory named ip.txt.

Alt Text

  • Now inside inventory put Managed Node username, Password and the connection type .

Alt Text

  • Now check the connectivity with Managed Node using the command ansible all -m ping

Alt Text

If it shows a message in green color that means there is connectivity and you will see the message ping pong.

  • Now create a directory mkdir /etc/ansible

Alt Text

  • Inside etc/ansible create ansible configuration file ansible.cfg and inside configuration file add the path of an inventory and set host key checking false so that it will not verify when we first time connect through ssh 👇

Alt Text

  • Now inside /etc/ansible directory create yml file, here i have created file name docker.yml. This yml file is playbook where we write code in YAML.

Now code inside docker.yml 👇

Alt Text

Alt Text

  • Now run the playbook using the command ansible-playbook docker.yml

Alt Text

Alt Text

Now check in Managed Node or Target node is changes made

  • Docker installed Alt Text

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

Alt Text

  • Docker repo created in /etc/yum.repos.d

Alt Text

  • httpd image launched in Docker

Alt Text

  • Now check IP of the container

Alt Text

  • Launched Webserver Alt Text

Thank You!!✨✨

Top comments (0)