Web Hosting on AWS With Ansible Using Dynamic Inventory Concept

Web Hosting on AWS With Ansible Using Dynamic Inventory Concept

Task -

Statement : Deploy Web Server on AWS through ANSIBLE!

??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. 

Lets something know about Ansible and AWS -

****Ansible is tool which is used for Configuration Management (CM). In Ansible we only tell to "what to do". We don't need to tell "How to do" because Ansible already knows that "How to do this task" on each type of Operating System .

****AWS is a cloud which is provided by Amazon. Amazon Elastic Compute Cloud (Amazon EC2 ) is a part of Amazon.com's cloud-computing platform, Amazon Web Services, that allows users to rent virtual computers on which to run their own computer applications. AWS EC2 service provisions resources like RAM , HardDisk . CPU etc.

To communicate with AWS we need -

> "boto" API

> Access key and Private key (To login in aws)

Step - 1 Install "boto" & "boto3" libraries -

No alt text provided for this image

I already have access key and private key to login in AWS.

Step - 2 Create Ansible Configuration File -

No alt text provided for this image

I have already "hello.pem" key for ssh login in my system. If you don't have then put here.

Step - 3 Create Dynamic Inventory -

Dynamic Inventory Directory -(In my case "inventory")

To create dynamic inventory download a program from this link for ec2 -

https://github.com/ansible/ansible/tree/stable-2.9/contrib/inventory

Command for this -

"wget https://github.com/ansible/ansible/tree/stable-2.9/contrib/inventory"

No alt text provided for this image

And change python interpreter to "#!/usr/bin/python3" -

No alt text provided for this image

Now set these environment variable - "AWS_REGION " , "AWS_ACCESS_KEY_ID" , "AWS_SECRET_ACCESS_KEY"

No alt text provided for this image

To check your inventory is working fine then run this command "ansible all --list-hosts" - (I don't have any instance at this time)

No alt text provided for this image

Now It's working fine.

Step -4 Setup Ansible Role For Launch AWS EC2 Instance-

(A) Create Role -

Create Ansible role for ec2 instance with "ansible-galaxy init ec2" . In my case my role name is "ec2".

No alt text provided for this image

(B) Task for launch ec2 instance -

Task -

No alt text provided for this image

Write variable for this task in "vars" directory (file "launch.yml") -

Security Group is allowing 22 , 80 , 443 , 85.

No alt text provided for this image


Write a Vault "credential.yml" for access key and secret key in vars directory -

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

(C) Task for Refresh Dynamic Inventory -

To change in initially loaded inventory we have to reload or refresh that inventory . For this "meta" module is available. To more about this module visit this link -

No alt text provided for this image


Step - 5 Setup Ansible Role for Configuring Apache Web Server -

(A) Create Ansible Role

Create Role with "ansible-galaxy init webserver" . In my case Role is "webserver"

No alt text provided for this image

(B) Task for install Apache web server -

No alt text provided for this image

Write variable in "vars" directory in "main.yml" -

No alt text provided for this image

(C) Task For Copying Web Configuration file -

No alt text provided for this image

I have web configuration file ("web.conf.j2") in "templates" directory

No alt text provided for this image

Write Variable in "vars" directory in "main.yml" file -

No alt text provided for this image


(D) Task for Creating DocumentRoot directory -

No alt text provided for this image

Variable is already in vars/main.yml file.

(E) Task For Download web file in DocumentRoot -

No alt text provided for this image

Write variable in vars/main.yml file -

No alt text provided for this image


(F) Task to start httpd service -

No alt text provided for this image

(G) Handler to restart httpd service -

Create handler to run it when anything changed in web.conf.j2 file. First For notify we have to use "notify" keyword in "Configure Web Server" task

No alt text provided for this image

"Restart Web Service" is name of handler.

Write handler in handler directory -

No alt text provided for this image

Step - 6 Create A Playbook To Run "ec2" and "webserver" Ansible Role -

(A) Create Playbook -

No alt text provided for this image

(B) Set role path in ansible configuration file -

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

Run the "deploy.yml" playbook -

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

Now its working fine -

No alt text provided for this image

Our task is successfully completed.

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

Govind Bhardwaj的更多文章

社区洞察

其他会员也浏览了