Webserver Configuration using Ansible and Docker
In this blog, we would :
- Configure and start Docker Services.
- Pull apache httpd server image from DockerHub.
- Run the docker container and expose it to public.
- Copy the html code in document root .
- Start the webserver.
And all the above steps would be performed on a separate remote system (or a VM) using Ansible.
Refer to my blog (given below) if you want to know more about what Ansible is and how it is used by the IT industry.
How Microsoft is using Ansible.
Automation has made setting up of infrastructure available on one’s fingertips. Before the age of automation and…
The Ansible architecture broadly consists of the following:
Control Node: Often known as the brain of any Ansible configuration, it is the device where Ansible is installed and is used by the user to control the managed nodes. Control node is the place where ansible plays are written by the user.
Managed Nodes: These are the devices/servers that are managed by Ansible. All the tasks are performed on these nodes.
Playbook: Ansible playbook contains a list of tasks that are performed on the managed nodes. It can be categorized as a type of “script”, which contains a number of plays. It is written in YAML format.
Inventory: It is a user created file which contains the details of the target/managed nodes. Details like IP address, username, password, connection type etc. are stored in this file. This file is responsible for establishing a successful connection between the control and managed nodes in order to perform any task.
In simple terms, Docker is a software platform used for building applications based on containers — small and lightweight execution environments that make shared use of the operating system kernel but otherwise run in isolation from one another. Containers are isolated from one another and bundle their own software, libraries and configuration files. All containers are run by a single operating system kernel and therefore use fewer resources than virtual machines.
That’s all the basics you need to know to dive into this task. So lets get started!
Step 1: Install Ansible in your local system/controller node. Here I am using Windows Subsystem for Linux as a local system. This provides a Linux environment inside a Windows machine without the need of running a virtual machine. The following commands would install and check the version of Ansible in your system. You would also need to install sshpass as it is a dependancy of Linux.
#to install Ansible
pip3 install ansible#to install sshpass
yum install sshpass#to check the version of Ansible installed in the system
Step 2: Create an inventory file (text file) and add the details of your managed nodes in the format given below:
<ip address> ansible_user=root ansible_ssh_pass=<password> ansible_connection=ssh
Now go to ansible configuration file and provide the location of your inventory file.
#to open ansible configuration file
Note: Create a folder at “/etc/ansible/” if not already created and follow the above step.
Step 3: Now that you’ve configured Ansible, check whether your managed nodes are connected and active.
#to check the list of managed nodes
ansible all --list-hosts#to check the connectivity
ansible all -m ping
Step 4: Now that we’ve successfully installed, configured and tested the connectivity, we can start writing the playbook for the tasks mentioned at the beginning of the blog.
#to create a playbook
Step 5: Running the playbook:
In the above picture, we can see that the playbook has been successfully executed and no error is encountered during the process.
We can do the following steps to verify that our task has been successfully executed:
- Docker configuration status in managed node:
systemctl status docker
2. Check the status of httpd image pulled in managed node:
3. Check whether the container is launched or not:
4. Check the deployment status of webserver launched in the container. We can access the webpage in two ways:
- Using the the IP address of the container.
#command to find the IP of container
docker inspect webserver1 | grep IP
2. Since we’ve used port forwarding, we can access the same webpage on the host machine (where container is running) using its IP address along with the port which is forwarded. Go to your browser and type this in the URL: