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
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.
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”
— name: “editing and pasting haproxy file”
— name: “starting ha proxy serice”
— name: “disabling selinux”
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”
— name: “putting index.php file in backend server”
— name: “starting haproxy server”
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
- name:calling mylb role to configure haproxy
- hosts: b
- name:calling myweb role to configure webserver
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 :)
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.