Configure Web Server on AWS Cloud using ANSIBLE
Nikhil Suryawanshi
MLOps Engineer | 7x GCP | Kubernetes | Terraform | AWS | DevOps | Java | Python
Task description-
Deploy Web Server on AWS using ANSIBLE
? Provision EC2 instance through ansible.
? Retrieve the IP Address of instance using dynamic inventory concept.
? Configure the web server through ansible! by copying the webpage to /var/www/html/ directory .
Let’s start -
For launching Instance on AWS Cloud from localhost, we have to install library for ansible to contact with AWS EC2 instance.
For this install boto library using pip3 command …
pip3 install boto pip3 install boto3
Write the code for launching instance & configuring webserver on the top of AWS Cloud .
Here I create two playbook one for launching AWS intance (web.yml) & second for configuring webserver (set.yml) into the instance .
Here I created two different role for launching instance & configuring webserver .
Now to retrieve the public IP of that instance Dynamically we have to create dynamic inventory .
For this we have to download two files from this two links .
Run the below command to download those two files . These are the scripts given redhat officially .
Make a diff. folder and then enter these URL's to download scripts. wget https://raw.githubusercontent.com/ansible/ansible/stable-2.9/contrib/inventory/ec2.ini wget https://raw.githubusercontent.com/ansible/ansible/stable-2.9/contrib/inventory/ec2.py Make both files executable by running this commands ... chmod +x ec2.py chmod +x ec2.ini
And then provide the AWS_REGION , AWS_ACCESS_KEY_ID andAWS_SECRET_ACCESS_KEY in the ec2.ini files
export AWS_REGION='ap-south-1' export AWS_ACCESS_KEY_ID='enter your access key' export AWS_SECRET_ACCESS_KEY='enter your secret key'
Now we have to edit inside ec2.py file. This file written in python 2 but we are using python 3 so we need to edit the header.
#!/usr/bin/python3
Now we have to make some changes in ansible configuration file .
Now run the playbook to launch instance on AWS -
ansible-playbook web.yml
Our AWS instance launched -
Dynamic inventory Dynamically Retrieve the public IP of the instance .
Checking that instance IP is pinging or not using ansible command ..
ansible all -m ping
Now Let's run the playbook to configure webserver into the instanced .
ansible-playbook set.yml
Now enter the public IP of that Instance -
Our output -
Github URL - https://github.com/NikhilSuryawanshi/Ansible_code/tree/main/task2
TASK COMPLETED!!!!
ThankYou For Reading .