Ansible role to configure Httpd WebServer and to configure HAProxy LB.

Sandeshjain
3 min readSep 7, 2022

Ansible role is a set of tasks to configure a host to serve a certain purpose like configuring a service. Roles are defined using YAML files with a predefined directory structure. A role directory structure contains directories: defaults, vars, tasks, files, templates, meta, handlers.

A load balancer intelligently distributes traffic from clients across multiple servers without the clients having to understand how many servers are in use or how they are configured. Because the load balancer sits between the clients and the servers it can enhance the user experience by providing additional security, performance, resilience and simplify scaling your website.

now we would create two roles in our controller node such that we could configure apache web servers and load-balancers.

We would add the roles_path in our configuration file to avoid specifying the role’s location again and again.

Now for the practical purpose we would enter IPs of atleast two web servers and a load-balancer for them into the inventory file.

Now we could see we have created two roles that are myapache and myloadbalancer respectively.

Then we have created a setup YAML file to run both the roles to configure our desired architecture.

Now we can see the main task file in the myapache where all the tasks to be done to configure the web servers are defined.

Now we can see the variables required are saved in other YAML file and the php file to copied to the managed node is kept in the file folder.

Now we have setup a similar coding for the myloadbalancer role wherein the template file is stored in the template folder. And again the variables are in separate YAML file in the vars folder. Here the handler is stored in the handler folder, ‘main.yml’ file.

The tasks for configuring the loadbalancer is defined in the main.yml fuile in the tasks folder.

Now we can see the setup.yml will call both the roles and configure the whole architecture at once.

Now we would run our Ansible roles to get HAproxy servers running.

Now we can clearly see that at socket 192.168.43.132:8081 first the server used is “192.168.43.61”.

On refreshing the page, on the same socket 192.168.43.132:8081, the server used now is “192.168.43.168”.

So finally our HAProxy server is set and has been deployed over internet.

Thank You

--

--