How To Make Ansible Role And Integrate them

Hey guys hope you all are doing good in today's article we are going to see how to create different ansible roles and Integrate them. Basically, roles are the way to manage the playbook properly. In real life scenario, we have to make a playbook for different service and if we make one playbook for all service so if something fails we have to go through all playbook but what if we maintain a separate playbook for separate service and whenever we need playbook for that service we can call it in our main playbook.
Here I am assuming you have gone through my configuring haproxy blog if not do read it first and then come back. Link provided below
https://gupta-aditya333.medium.com/setting-haproxy-using-power-of-ansible-54197677046a
Let's start with practical
First of all goto to the ansible configuration file and there specify the path of the role using roles_path.
Eg:-
roles_path= your path
One path is specified go to that path and run the below command
ansible-galaxy role init “name of role you want to give”
For today's practical we are going to create two roles one to configure web server and one to configure haproxy.

In the above image, you can see we have created two roles one mylb for configuring haproxy and one myweb to configure webserver.
First, let's create a role for setting up a haproxy load balancer. After you have created a role go inside that and head towards the folder name task inside that make one file with the name main.yml as you can see in the above image inside that file put the below code.
— -
# tasks file for mylb- name: “Installing haproxy”
yum:
name: “haproxy”
— name: “editing and pasting haproxy file”
template:
src: haproxy.cfg
dest: /etc/haproxy/haproxy.cfg
— name: “starting ha proxy serice”
service:
name: “haproxy”
state: restarted
enabled: yes
— name: “disabling selinux”
ansible.posix.selinux:
policy: targeted
state: permissive
A complete explanation of the code is given in my haproxy configuring article.
Once you have done editing the main.yml file save it and close and copy Hadoop. cfg file in the template folder as shown in the above image we have to edit the configuration in the same manner as we did in previous article.
Once all these steps are done our one role is ready for configuring the haproxy load balancer.
Now create a new role for configuring web server for this role also follow same steps till creating file main.yml inside task folder and put the below code
— -
# tasks file for myweb- name: “installing http and php”
package:
name:
— httpd
— php
state: present
— name: “putting index.php file in backend server”
copy:
src: index.php
dest: /var/www/html/index.php
— name: “starting haproxy server”
service:
name: httpd
state: started
Once done with writing code save it and close it and head towards the file folder and there make file name called index.php(here I am taking index.php you can take any file name you want just change it in src in the above code).
Wohooo🥳🥳 That's it second role is also created.
Now, whenever we want to configure a web server or haproxy load balancer we just have to call them Thinking How?? Let’s see how to call them and integrate them because we want them both to run one after the other.
create one playbook with any name as you want wherever you want as we did in our last articles inside that playbook put the following code
- hosts: localhost
tasks:
- name:calling mylb role to configure haproxy
include_role:
name: mylb
- hosts: b
tasks:
- name:calling myweb role to configure webserver
include_role:
name: myweb

To check go to your IP on port 5000 you can see haproxy working.
Guys, here we come to the end of this blog I hope you all like it and found it informative. If have any query feel free to reach me :)
Github link:-https://github.com/guptaadi123/ansible-roles.git
Guys follow me for such amazing blogs and if have any review then please let me know I will keep those points in my mind next time while writing blogs. If want to read more such blog to know more about me here is my website link https://sites.google.com/view/adityvgupta/home.Guys Please do not hesitate to keep 👏👏👏👏👏 for it (An Open Secret: You can clap up to 50 times for a post, and the best part is, it wouldn’t cost you anything), also feel free to share it across. This really means a lot to me.