Use Ansible playbook to Configure Reverse Proxy i.e. Haproxy and update its configuration file automatically each time a new Managed node (Configured With Apache Web Server) joins the inventory. Do setup on AWS Cloud Using Dynamic Inventory.
HAPROXY — HAProxy is open-source software that helps in load balancing. It improves speed and makes it efficient. It does reverse proxy means when the client sends a request to load balancer it creates another request and sends it to the webserver.
STEP 1 -
first Install ec2.py and ec2.ini files from this link — https://github.com/ansible/ansible/tree/stable-2.9/contrib/inventory
Otherwise, use these links to download directly on Redhat Linux operating system — https://raw.githubusercontent.com/ansible/ansible/stable-2.9/contrib/inventory/ec2.py
https://raw.githubusercontent.com/ansible/ansible/stable-2.9/contrib/inventory/ec2.ini
Use command -
- wget https://raw.githubusercontent.com/ansible/ansible/stable-2.9/contrib/inventory/ec2.py
- wget https://raw.githubusercontent.com/ansible/ansible/stable-2.9/contrib/inventory/ec2.ini
STEP 2-
Download boto by using the command “ pip3 install boto ”.
STEP 3 -
Install ec2 package by using “ pip3 install ec2 ”.
STEP 4 -
Set your IAM user Access Key and Secret Key.
NOTE — don’t use the root account access key and secret key and don’t share your keys with anybody.
The keyword used to set key is -
“ export AWS_ACCESS_KEY_ID=’XXXXXXXXXXXXXXXX’ “
“ export AWS_SECRET_ACCESS_KEY=’XXXXXXXXXXXXXXXXXXXXXX’ ”
STEP 5 -
Setup ansible.cfg file by using below code -
- Give path of dynamic inventory (where you kept ec2.py and ec2.ini file )
- Set host key checking to false.
- Set remote user as ec2-user
- Give the path of the private key
- Give privileges escalation
STEP 6 -
Change the access permission of the key file by using the command “ chmod 400 key.yml “.
— — — — — — — — — — — — — — — — — — — — — -
CODE -
Ec2.yml-
Haproxy.yml -
Variable.yml-
— — — — — — — — — — — — — — — — — — — — — -
ALL THINGS ARE SET NOW WE CAN RUN THE ANSIBLE CODE -
TIME TO SECURE THE KEY -
By using the vault command we can secure our critical data or credentials. Here I have secured my access key and secret key.
The command to secure data is — “ ansible-vault create key.yml ”
It will ask for a password so set a password.
LAUNCH EC2 INSTANCE -
While launching ec2 instances I am launching one instance as a load balancer and two instances as a web server.
The command to launch the ec2 instance is — “ ansible-playbook — ask-vault-pass ec2.yml ”
LoadBalancer and Backend Servers are launched successfully -
LOAD BALANCER -
WEB SERVER -
CHECK DYNAMIC INVENTORY HOSTS -
As Dynamic inventory is picking up IPs in real-time.
The command to check the host is — “ ansible all — list-hosts ”.
As we have given tags to each instance so we can check hosts by using tag names.
Command to check hosts by tag name is — “ ansible tag_Key_Value — list-hosts ”
LOAD BALANCER-
WEB SERVER-
VOLUME ATTACHED TO INSTANCES-
By default, volume is created.
LAUNCH REVERSE PROXY SETUP-
The command to launch a playbook in — “ ansible-playbook file.yml ”
The command to show extra details while launching setup is — “ ansible-playbook -v file.yml ”
HAPROXY CONFIG FILE-
After launching haproxy the webserver IP will be updated dynamically.
CHECK THE WEBSITE -
IP:8080 — write this in the search bar and that will open the webserver.
SEE OUR CODE HAS TRANSFER FILES -
LOAD BALANCER -
WEB SERVER -
Thank You for reading this article!!
Github URL — https://github.com/manali1230/ANsible_haproxy_aws_dynamic_inventory.git