Provisioning ec2 instance on AWS and Configuring Load balancer and Apache Webserver using Ansible Dynamic Inventory
👉🏼👉🏼👉🏼 Already I have published one article about how to configure load balancer and apache webserver . But the limitation of this article is that I had used static inventory . That’s why I am here again with new blog to overcome this limitation .
I have launched aws instances and fetched public ip of new instances . Not only that , I have configured reverse proxy and webserver as per requirement . All has been done using Ansible.
📢📢📢 DYNAMIC INVENTORY SETUP
you will learn how to set up a dynamic inventory on AWS using boto3 and boto, ec2.yml and ec2.ini file.
Follow the steps carefully for the setup.
step 1:
- Install python3 & boto3 and boto library using the following command :
yum install python3 -y
pip3 install boto3
pip3 install boto
step2:
- create a directory “ mkdir /Ansible_files/task12.2/inventory ”. In my case I made it here but you can make it anywhere. All we have to do is write to their configuration file where the ec2.py module is located.
- download ec2.yml and ec2.ini from ansible official dynamic inventory GitHub link in my inventory folder. both the files should be in the current folder .
[Download ec2.py inventory ]
wget https://raw.githubusercontent.com/ansible/ansible/stable-2.9/contrib/inventory/ec2.py
[Download ec2.ini inventory ]
wget https://raw.githubusercontent.com/ansible/ansible/stable-2.9/contrib/inventory/ec2.ini
Now to make your ec2.py executable we need to use one command
chmod +x ec2.py
- Open the ec2.py file and change the env Python to Python 3 on the first line because this Python code is written in Python 2 but you can use it if you already have Python 2 interpreter installed.
In the next step, we just need to set the environmental variable to your aws_access_key_ID and secret_ access_key. For this we can use the export command.
export AWS_REGION=’ap-south-1'
export AWS_ACCESS_KEY_ID=’XXXXXXXXX’
export AWS_SECRET_ACCESS_KEY=’xxxxxxxxxxxxxxxxxxxxxxxxx’
Note: After setting this environmental variable simply go to the /root/.bashrc file and update this entry because every VM reboots, this environmental variable is automatically lost.
Now update this entry “/ansible_file/task12.2/inventory” directory in ansible configuration file(/etc/ansible/ansible.cfg) and also set aws private_key and user through which you wanted to launch os on aws.
I haven’t updated the IP instance of any config or ip.txt file because the reply automatically creates dynamic inventory so we don’t have to.
LAUNCHING INSTANCE ON TOP OF AWS USING ANSIBLE PLAYBOOK
Ansible playbook to launch 3 instance on top of aws cloud.
Let’s run the playbook :
In my case, I don't use the external file named to attach aws_access_key and aws_secret_key. You can attach the var file for security reason.
Output of this code :
giving instance-tags are very important, we will use it to dynamically configure the haproxy file otherwise it has failed.
we can list all the details about all the instances using “$ ./ec2.py “.
This tag_name helps us configure web server setup and hapoxy setup.
Configuration Reverse Proxy with webserver
Ansible-playbook to configure the apache setup and webserver setup
And this is the hypoxy.cfg file that I wanted to configure with Apache . Update the entry with instance_tag_name otherwise it won’t work.
Now that everything is configured properly, the answer-playbook is being run to set up Hypoxy and Webserver above the top cloud.
⭐️ Final outcome ⭐️
we have configured haproxy in <tag_name_loadbalancer> and this contain load balancer IP which is 13.232.171.227
Now browse http://13.232.171.227:8080/ multiple time .
✔️✔️✔️️I wrote a PHP code to print the IP address of that system. If we browse more than once, it will give a different IP each time, which means the load balancer is working well.
🔶🔶 Thanks for reading ……🔶🔶
🔶🔶 keep sharing … 🔶🔶