Creating Two roles which can create and control webserver versions and solve challenge for host ip’s addition dynamically over each Managed Node in HAProxy.cfg file.
Before starting with the practical we should know something about
What is Roles in ansible?
Roles let you automatically load related vars_files, tasks, handlers, and other Ansible artifacts based on a known file structure. Once you group your content in roles, you can easily reuse them and share them with other users. Role directory structure. Storing and finding roles.
How Ansible playbook role Benefit us ?
They provide a skeleton for an independent and reusable collection of variables, tasks, templates, files, and modules which can be automatically loaded into the playbook. Playbooks are a collection of roles. … Instead, you can create 10 different roles, where each role will perform one task.
Here in this Task, I have created two roles.
wp1 , wp2
First one to Create and launch a Webserver
Second one to install Haproxy and configure haproxy configuration file with all the webserver .
In My First Role
I assigned tasks inside my roles Task folder
In copy module, We need to copy a file which I stored inside my roles copy folder .
In my second role ,
I assigned tasks inside my roles Task folder
In template module, We need to parse a file which I stored inside my roles template folder in which I used jinja for some reason ,to make it dynamic.
Now, We are completed with Roles ,
Next step is, I updated my ansible configuration file .
Here I have updated with the path where my roles is stored .
80% of our work is Done ,We just need to create a playbook and there you go .
Let’s check our Inventory file
I created a Playbook with name setup3.yml
In this playbook ,I added 2 plays
One is for Webserver
I provided hosts as my webserver Ip group present in my inventory and the Role I created for webserver .
Second is for Haproxy.
I provided hosts as my haproxy Ip group present in my inventory and the Role I created for haproxy .
Running My playbook
Finally ,We can see my Website is launched with a Load Balancer at frontend and server at the backend .
Checking which ports are in listening from the Web Server terminal ,ie Backend Server
netstat -tlnp
Checking which ports are in listening from the Haproxy Server terminal ,ie Frontend Server.
netstat -tlnp
We can check our Web server is launched .