
Configure WebServer on AWS Cloud using automation tool called ANSIBLE
Here is my second task given by Vimal Sir in the RedHat Ansible training program … In this task, I’m going to configure webserver on the top of AWS Instance using Ansible tool …
📝 Problem Statement : 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 solve this case step by step …
Step 1: 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
After installing boto , boto3 use this command to cross check whether libraries are installed or not …

Step 2: Write the code for launching webserver on the top of AWS Cloud … Here is the code ..
- hosts: localhost
vars_files:
— credentials.yml
tasks:
— name: Launch VM on AWS Cloud
ec2:
key_name: "hcc81"
instance_type: "t2.micro"
image: "ami-052c08d70def0ac62"
wait: yes
count: 1
vpc_subnet_id: "subnet-1f3c4953"
assign_public_ip: yes
region: "ap-south-1"
state: present
group: "default"
aws_access_key: "{{ awsuser }}"
aws_secret_key: "{{ awspass }}"
instance_tags:
Name: Ansible_aws_wb
Access key and secret key are very confidential for us … So, In ansible we have a concept of vault … Using that concept create another file. Here is the command :
ansible-vault create --vault-id prod@prompt credentials.yml

The credentials file which I have created is fully encrypted by ansible … Check using this cmd :
cat credentials.yml

Step 3: Run the playbook using below command …
ansible-playbook --vault-id prod@prompt task.yml

Check AWS Console whether the instance is launched or not with desirable parameters …

Step 4: Now retrieve the public IP of that instance which I have launched before … For this u 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 … Use can check this from ansible docs …
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.iniwget https://raw.githubusercontent.com/ansible/ansible/stable-2.9/contrib/inventory/ec2.pyMake both files executable by running this commands ...chmod +x ec2.py
chmod +x ec2.ini
We have to enter the region , aws access key and aws secret key in that files … Doing that one is diificult , So export the below these things in command line as I mentioned below ..
export AWS_REGION='ap-south-1'
export AWS_ACCESS_KEY_ID='enter your access key'
export AWS_SECRET_ACCESS_KEY='enter your secret key'
After exporting run the below command to retrieve the current running Instances IP’s in AWS Cloud …
./ec2.py --list
U can see the IP of current running Instance in AWS Cloud …

U can also cross check with the console of AWS Cloud …
Now create a normal file and enter the IP of EC2 instance which we have retrieved using Dynamic Inventory concept … Here is what I have did ..

I have already copied the key pair to root folder …
Step 5: Now we have to make some changes in ansible configuration file … Here I’m sharing whole code to u …

Check that instance IP is pinging or not using ansible command ..
ansible all -m ping

Step 6: Now write another playbook for configuring webserver on AWS Instance … Here is the code …
- hosts: all
tasks:
- name: Install httpd package
package:
name: "httpd"
state: present - name: Copy index.html webpage
copy:
src: "index.html"
dest: "/var/www/html" - name: start the httpd service
service:
name: "httpd"
state: started
Run the playbook using below command …
ansible-playbook ec2web.yml

Now enter the public IP of that Instance after running above playbook …
Output :

Without going to AWS Instance , we successfully configured webserver using ansible tool …
Thanks for reading !!!
GitHub Link : Click here for code
…. Signing Off ….