Configure Web Server on AWS Cloud using  ANSIBLE

Configure Web Server on AWS Cloud using ANSIBLE

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 .

No alt text provided for this image

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 .

No alt text provided for this image
No alt text provided for this image
No alt text provided for this image

Now run the playbook to launch instance on AWS -

ansible-playbook web.yml

No alt text provided for this image


No alt text provided for this image

Our AWS instance launched -

No alt text provided for this image

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

No alt text provided for this image

Now Let's run the playbook to configure webserver into the instanced .

ansible-playbook set.yml


No alt text provided for this image

Now enter the public IP of that Instance -

Our output -

No alt text provided for this image

Github URL - https://github.com/NikhilSuryawanshi/Ansible_code/tree/main/task2

TASK COMPLETED!!!!

ThankYou For Reading .

要查看或添加评论,请登录

Nikhil Suryawanshi的更多文章

社区洞察

其他会员也浏览了