ONE CLICK PROVISIONING AWS EC2 WITH ANSIBLE ROLE ! (webserver and load balancer)

Shubhyansh Rai
4 min readJan 12, 2021

GOAL:

🔅Provision EC2 instances through ansible.

🔅 Retrieve the IP Address of instances using the dynamic inventory concept.

🔅Configure the web servers through the ansible role.

🔅Configure the load balancer through the ansible role.

🔅The target nodes of the load balancer should auto-update as per the status of web servers.

  • PROVISIONING EC2 INSTANCE

— read this for details about the provisioning of ec2 instance believe me it will be really easy for you afterwards

— now everything is same as mentioned in above blog but this time we will make two instance for web server

(NOTE: create a ansible vault file to store your ec2 credentials )

— similarly we will create instance for load balancer also:-

(NOTE: the instance for load balancer must be created after the ip’s of webserver is dynamically retrived for management perspective)

  • RETRIVING IP DYNAMICALLY

— with boto installed use the below mentioned command to retrive the ip dynamically

— this process is same for saving load balancer instange in its host group

— after inicializing the loadbalancer instance we will retrive its ip as follows:-

  • CREATING ROLE FOR WEBSERVER

— to create the role type the following command :-

— now go to the directory where you save your roles and change directory to httpdsweberver/tasks/

and then edit the main .yml file in it as follows:-

(NOTE: you must have php installed on your controller node and inside my.php write code to show ip of managed node)

  • CREATING ROLE FOR HAPROXY

— to create role for ha proxy again type the below code

— now go to the directory where you save your roles and change directory to loadbalancer/tasks/

— inside the main.yml file of the role type the following code

NOTE : the haproxy.cfg .j3 file must be edited and saved at the same place as roles inside it change the bind port to 8081 or as required and write a jinja for loop to get ip of webservers in it also there is a handler in this role:-

change in configuration file of ha proxy
HANDLER
  • RUNNING AND TESTING

— type the below code and then run the whole play book in ansible

NOW TEST THE SETUP BY TYPING THE IP OF LOAD BALANCER IN THE CROME TAB:

--

--