Ansible Role !!

Task Descriptionđź“„

🔸Create an ansible role myapache to configure Httpd WebServer.

🔸Create another ansible role myloadbalancer to configure HAProxy LB.

🔸Memory management We need to combine both of these roles controlling webserver versions and solving challenge for host ip’s addition dynamically over each Managed Node in HAProxy.cfg file.

  1. Ansible is simple open-source IT engine which automates application deployment, intra service orchestration, cloud provisioning and many other IT tools.
  2. Ansible uses playbook to describe automation jobs, and playbook uses quite simple language i.e., YAML (It’s a human-readable data serialization language & is commonly used for configuration files but could be used in many applications where data is being stored) which is very easy for humans to understand, read and write. Hence the advantage is that even the IT infrastructure support guys can read and understand the playbook and debug if needed (YAML — It is in human readable form).
  3. Ansible is designed for multi-tier deployment. Ansible does not manage one system at time, it models IT infrastructure by describing all your systems are interrelated. Ansible is completely agentless which means Ansible works by connecting your nodes through SSH (by default). But if you want other method for connection like Kerberos, Ansible gives that option to you.

In Ansible, there are two categories of computers: the control node and managed nodes. The control node is a computer that runs Ansible. There must be at least one control node, although a backup control node may also exist. A managed node is any device being managed by the control node.

Ansible works by connecting to nodes (clients, servers, or whatever you’re configuring) on a network, and then sending a small program called an Ansible module to that node. Ansible executes these modules over SSH and removes them when finished. The only requirement for this interaction is that your Ansible control node has login access to the managed nodes. SSH keys are the most common way to provide access, but other forms of authentication are also supported.

HAProxy is a free, amazingly fast and reliable solution offering high availability, load balancing, and proxying for TCP and HTTP-based applications. It is particularly suited for extremely high traffic web sites and powers quite a number of the world’s most visited ones. Over the years it has become the de-facto standard opensource load balancer, is now shipped with most mainstream Linux distributions, and is often deployed by default in cloud platforms. Its mode of operation makes its integration into existing architectures very easy and riskless, while still offering the possibility not to expose fragile web servers to the internet.

✦ Pre-Requisites:

1.Installed boto/boto3 in controller node

2.AWS account

3.Installed Ansible

4. Installed HAproxy in controller node

Here I’m checking the ansible version :

command : ansible — version

Now we have to go at managed node.. and check the IP of the managed node . When we get the IP of managed node then go to the controller node and create the inventory file .

command : vim <file_name>.txt

After creating the inventory , we have to create the ansible configuration file by using the command

command : vim /etc/ansible/name_of_file.cfg

Check the connectivity between the controller node and target nodes using the following command:

Command : ansible all -m ping

Create the role path and then we have to add that path into the ansible configuration file

Here myroles path is created.

After that create two roles i.e. myapache and myloadbalancer

command : ansible-galaxy init <role_name>

Here you can see two roles are created successfully.

After creating the roles go into the myapache roles in that go to the tasks folder and add the tasks into the main.yml file

After that go the handler folder and add the handlers into the main.yml file

Now lets go to the myloadbalancer roles , Into the myloadbalancer go to the tasks folder and then add the required tasks for configuration of haproxy into the main.yml file

After that go the handler folder and add the handlers into the main.yml file

Now go to haproxy.cfg file and add the code which can dynamically add the IP’s into the configuration file of haproxy .

Here I am using jinja2 embedded code for dynamically fetch or register the new webserver IP with HAproxy loadbalancer.

Create index.php file add the following code in the file and save it:

Command : vim index.php

Now create one playbook which can run all this task together . Here I am creating a one playbook which can run both the roles

Now run the playbook and lets see what happen ?

And yes !! Playbook run successfully ! and we have successfully configure the loadbalancer !!

Now, let’s check the outcome by visiting the HAproxy server IP with HAproxy port number .

Here have you notice , the IP is switching even though the IP address which I am using at browser is same. This proves that the HAproxy is working fine.

Now lets add one more IP address into the inventory file to check the handler added into the myloadbalancer role is working or not

Previously, we only had 2 target nodes, but now, we have 3.

Now copy the another index.html into the /var/www/html folder to check the handler added into the myapache role is working or not ?

Now Let’s check the connectivity between the controller node and target nodes using the following command:

Command : ansible all -m ping

Let’s run the playbook again and check if the HAproxy server automatically adds the new target node in its configuration file or not.

Here, as you can see, the tasks are changed for the new target node. Now, let us visit the HAproxy once again and check if it has recognized and updated its configuration file with the new target node or not.

From the above output , you can see that 3 IPs are switching, which means that the third target node is also automatically added in the configuration file of HAproxy and new web page also copied into the target node .

YES !!! We have successfully solved the challenge!!




Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store