Configure The LoadBalancer On EC2 Instance !!

Shashwat Singh
3 min readMar 29, 2021

Lets Start😎 !!

Lets Write an Dynamic Playbook In Such a way we can lunch the Instance as Per Our Need.
→ Launch the 3 Instances for the Web-Server -

- hosts: localhost
become: False
vars_prompt:
- name: noOS
private: no
prompt: "How Many Instances Do You Wants Lunch ?"
- name: tag
private: no
prompt: "Give the tag Your Instance !!"
vars_files:
- awsAccess.yml
tasks:
- name: Launch instance for webserver
ec2:
count: "{{ noOS }}"
group_id: "sg-0d4c5f0d452e1b620"
image: "ami-08e0ca9924195beba"
aws_access_key: "{{ usern }}"
aws_secret_key: "{{ pass }}"
instance_type: "t2.micro"
key_name: "keyboard_shift"
region: "ap-south-1"
wait: yes
assign_public_ip: yes
instance_tags:
Name: "{{ tag }}"
vpc_subnet_id: "subnet-1fed9453"
  • count — will help us to run the number of instance according to our need
  • aws_access_key && aws_secret_key — Now this will be provided when you will create the IAM role for the user . We need provide power to launch or configure the instance.

Now For Directly Providing the Access_Key and Secret_Key in the Playbook is not an Good Idea 😎 .
Here, We can take the help of ansible vault . In this feature of ansible we can protect our precious data . Here are the Steps to Keep Your Data Safe !!

$ ansible-vault create file_name.yml
Ask For the Password!
-> Now Here We can Write give our Access_Key and Secret_Key.
You Can Store that password in hidden file so that at the run time only you can provide that file to the playbook. So, that we need not to enter the password again and again.--vault-password .vaultpass

Run the Playbook !

$ ansible-playbook --vault-pass .vaultpass playbook.yml
WebServer Instances Lunched
LoadBalancer Instances Lunched

2. Write the PlayBook For Configuring the WebServer && LoadBalancer.

In this Case I will use role as you know it just like plug in play which is easy to use.
More About Role →

For Capturing the IP we can take the Help of Dynamic Inventory . In this case tag play an important role.

Some Demos —

Retrieving IP according to the Tag

Now, At the Configuration File We can retrieve the Application IP’s by the help of tag_Name_web.

Lets Run The Role’s :

--

--