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.
So what are Ansible roles?
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.
So let's begin by updating inventory as well as configuration file of Ansible
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.
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
Here we have defined roles path also, it’s the path from where ansible will read my roles to run the playbook
Creating a role for HAProxy(Load Balancer)
So for creating a role we have to use the ansible-galaxy command in my workspace
ansible-galaxy init <name>
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.
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
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
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.
Creating a role for Apache Webserver(backend server)
We will again use the same command to create the role for the webserver
Let's write the code for webserver configuration in the task folder of the role created mywebserver.
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
Creating the playbook
Now we will create a playbook which will include these roles we created above and using these roles we will set up our environment.
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
There was no error in our playbook it ran successfully and now let's check if the load balancer is working or not
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