Configuring HAProxy and Apache web server using Ansible roles

Image for post
Image for post

Ansible is a handy configuration management tool as we have already looked in my previous articles. It helps in configuring a large number of the servers from one single controller instance. It helps automate complex tasks like configuration management, application deployment, creating CI/CD pipeline etc. Writing ansible code to manage the same service for multiple environments or different products increase code redundancy. With more complexity in functionality, it becomes difficult to manage everything in one ansible-playbook file. Sharing code among teams become difficult. Ansible Role helps solve these problems.

In this article, I would be configuring 1 system as haproxy(load balancer ) and 2 system as an apache web server but with using ansible roles

There is some concept which I have explained in my previous article about this setup using playbook so for better understanding read that article also using this link.

Ansible role is an independent component which allows reuse of common configuration steps. Ansible role has to be used within the playbook. 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. Each directory must contain a main.yml file which contains relevant content.

  • defaults: contains default variables for the role. Variables in default have the lowest priority so they are easy to override.
  • vars: contains variables for the role. Variables in vars have higher priority than variables in the defaults directory.
  • tasks: contains the main list of steps to be executed by the role.
  • files: contains files which we want to be copied to the remote host. We don’t need to specify a path of resources stored in this directory.
  • templates: contains file template which supports modifications from the role. We generally use the Jinja2 templating language for creating templates.
  • meta: contains metadata of role like an author, support platforms, dependencies.
  • handlers: contains handlers which can be invoked by “notify” directives and are associated with service.

I have created my inventory in “/root/ip.txt” location of the Controller Node, and it will mainly consist of few details about Target Node.

Image for post
Image for post

Here I have created a group of web server now we will use this name instead of IP address in the ansible-playbook. We will use our local system as a load balancer and we don’t have to include its IP in the inventory as it is pre-created in the inventory in the name of localhost.

Now let's just update configuration file of ansible as well

vim /etc/ansible/ansible.cfg

Image for post
Image for post

Here we have defined roles path also, it’s the path from where ansible will read my roles to run the playbook

So for creating a role we have to use the ansible-galaxy command in my workspace

ansible-galaxy init <name>

Image for post
Image for post

So the role for haproxy configuration is created now we will write the play according to our use case, it is a folder and inside this folder different folder is created for different purposes for better management of the code, so we will write our task in the file present in the task folder.

Image for post
Image for post

The configuration file the haproxy is stored in the template as it is dynamic and to be parsed.

Here is the configuration that I have done in that file

Image for post
Image for post
Image for post
Image for post

The variable which is used here is stored in the vars folder in the role so let's check what is present in that file

Image for post
Image for post

In this, we mentioned the port on which our load balancer will work and the port where my load balancer will reverse proxy our request to the webserver and in my case the load balancer is running on the port 8080 and backend server will run on the port 80.

We will again use the same command to create the role for the webserver

Image for post
Image for post

Let's write the code for webserver configuration in the task folder of the role created mywebserver.

Image for post
Image for post

Here we have mentioned all my task which I want to perform in the target node and the index.html file is stored in the files folder as it is static.

By this, we have completed the configuration of the roles

Now we will create a playbook which will include these roles we created above and using these roles we will set up our environment.

vim roles.yml

Image for post
Image for post

Here we just have to mention the name of the role and on which group we want to apply for that role like I have applied myhaproxy role on my localhost whereas mywebserver on myweb group which is formed in the inventory file.

So let's run the playbook and check for the error

ansible-playbook roles.yml

Image for post
Image for post

There was no error in our playbook it ran successfully and now let's check if the load balancer is working or not

Image for post
Image for post

So our roles and playbook created is working fine

Here is the Github link of the playbook and roles used above.

The interesting thing of using the role is that it made our code easy to manage as now we don't have to write these much lines again for the different group we can just add the name of roles and it will configure the according to the code written in that role and it made our code simpler to understand as well.

Thanks for reading this article! Hope You learned Something new

I would be writing more about Ansible so stay tuned

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