CONFIGURING WEBSERVER ON AWS EC2 INSTANCE USING ANSIBLE

CONFIGURING WEBSERVER ON AWS EC2 INSTANCE USING ANSIBLE

HELLO GUYS THIS IS RAGHAV TIWARI ONCE AGAIN ,

IN THIS ARTICLE I AM GONNA SHOW YOU HOW TO CONFIGURE AND LAUNCH THE APACHE WEBSERVER USING ANSIBLE FOR DOING THIS I AM USING REDHAT ENTERPRISE LINUX VERSION 8(RHEL8) AS A CONTROLLER NODE AND AWS EC2-INSTANCE AS MANAGED NODE

STEPS THAT I WILL :

♦️Provision EC2 instance through ansible.

♦️Retrieve the IP Address of instance using dynamic inventory concept.

♦️Configure the web server through ansible!

♦️Create role for webserver to customize the Instance and deploy the webpage to root directory. 

PREREQUISITES TO UNDERSTAND THIS ARTICLE-

-READER SHOULD HAVE GOOD KNOWLEDGE OF LINUX TRMINAL, YML LANGUAGE, HAVE GOOD IDEA ABOUT PLAYBOOK AND ROLES IN ANSIBLE

---------------------------------------------------------------------------------------------------------------

SO LETS GET STARTED::

STEP1: PROVISINING EC2-INSTANCE USING ANSIBLE

  • FOR THIS STEP FIRST OF ALL I WILL CREATE A ROLE FOR THIS IN OUR CONTROLLER NODE i.e. RHEL8 , S YOU CAN SEE IN THE BELOW SCREENSHOT
No alt text provided for this image
  • IN THIS ROLE NOW WE WILL MOVE THIS ROLE TO ONE OF MY FOLDER NAMED myroles JUST FOR MANAGEMENT PURPOSE
  • IN BELOW PIC YOU CAN SEE THE FOLDER NAMED AS <ec2provisioning> IS MY ROLE FOLDER FOR EC2 PROVISIONING IN THIS FOLDER WE HAVE MANY DIFFERENT TYPES OF FOLDER NAMED AS tasks, handlers, templates, files, etc. FOR EC2 INSTANCE LAUNCH I HAVE USED ONLY TWP OF THEM i.e. tasks folder WHERE IN main.yml FILE OUR MAIN CODE IS PRESENT WHICH CONTAINS THE CONFIGURATION CODE AND IN vars folder AGAIN THERE IS main.yml WHICH CONTAINS THE USER ACCESS KEY AND SECRET KEY OF IAM USER FOR LOGIN PURPOSE.


No alt text provided for this image
  • LETS HAVE A LOOK IN THE tasks and vars FOLDER :::

1.) tasks folder::

-main.yml file:

No alt text provided for this image

2.) vars folder: it contains credentials for login into AWS account in increpted format using concept of vault in ANSIBLE. so i will just show you the file location

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


NOW LETS RUN THIS ROLE AND LAUNCH OUR EC2-INSTANCE.................

-TO LAUNCH THIS ROLE WE HAVE TO CREATE AN EXTRA FILE OF YML FORMAT

No alt text provided for this image
  • LETS RUN THIS FILE-

(the screen background is white becoz i am accessing my Controller node from putty)

No alt text provided for this image

---------------------------------------------------------------------------------------------------------------NOW COMES THE MAIN WE HAVE LAUNCHED OUR EC2-INSTANCE SUCCESSFULLY NOW WE HAVE TO RETRIVE THIS INSTANCE IP WITHOUT GOING TO AWS LETS SEE HOW,

STEP2: Retrieve the IP Address of instance using dynamic inventory concept.

ANSIBLE COMMUNITY HAS DEVELOPED AND PROVIDED SOME PRECREATED CODE IN PYTHON (ansible is also a software built on python as a base)

to use this python code first we have to install boto liberary of pythonto use this python code first we have to install boto liberary of python

No alt text provided for this image

---PYTHON CODE ON GITHUB WHICH WE WILL USE TO DYNAMICALLY RETRIVE THE IP ADDRESS

No alt text provided for this image

github link for this python code ::: click me

- lets download this code and retrieve the ip address

-this code is in location < /cloudhost > folder

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

-IN ABVE PIC WE ALSO HAVE MADE THIS COMMAND EXECUTABLE THIS RED ENCIRCLED COMMAND WILL RETRIEVE ALL THE INFO ABOUT THE EC2 INSTANCE WE LAUNCHED ON AWS

No alt text provided for this image

-HERE IS THE IP ADDRESS IN THE ABOVE COMMAND OUTPUT

No alt text provided for this image

AFTER LAUNCHING EC2 WE HAVE TO GIVE ROOT POWER TO THIS INSTANCE FOR THIS WE HAVE TO EDIT sshd_config file and sudoers file and i have done this part manually

---------------------------------------------------------------------------------------------------------------

STEP3: Configure the web server through ansible! and creating role for webserver to customize the Instance and deploy the webpage to root directory. 

-AGAIN FIRST OF ALL I HAVE TO CREATE ROLE FOR CONFIGURING WEBSERVER

-I HAVE ALREADY CREATED ROLE FOR WEBSERVER ALSO AS YOU CAN SEE IN THE ORANGE BOX

No alt text provided for this image

-LETS GET INSIDE THIS FOLDER AND HAVE A LOOK

1.) LOCATION OF WEBSERVER

No alt text provided for this image

2.) tasks folder

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

3.) handlers folder

No alt text provided for this image

4.) vars folder

No alt text provided for this image

5.) templates folder

No alt text provided for this image

NOW LETS LAUNCH THIS ROLE AGAIN WE HAVE TO CREATE A SMALL YML FILE TO INITIATE ITS LAUNCH:

-host.yml

No alt text provided for this image

-LET US ALSO TAKE A LOOK AT ANSIBLE CONFIGURATION FILE:

No alt text provided for this image



-LAUNCHING THIS PLAYBOOK

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

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

NOW ITS TIME TO SEE THE OUTCOME OF ABOVE TWO ROLES, HERE WE GO

1.)EC2 INSTANCE LAUNCH SUCCESSFULLY

No alt text provided for this image

2.)WEBSERVER CONFIGURED AND WEBPAGE LAUNCHED SUCCESSFULLY

No alt text provided for this image

webpage dir root location changed

No alt text provided for this image

-WEBPAGE ON GOOGLE

No alt text provided for this image

-----------------------------------THANK YOU---------------------------------------------------------------------------------I HOPE YOU LIKED MY WORK ---------------------------------------------









To view or add a comment, sign in

More articles by Raghav Tiwari

Others also viewed

Explore content categories