Deployment of Webserver on AWS using Ansible

Deployment of Webserver on AWS using Ansible

Deployment of Webserver on AWS through Ansible

TASK 2

1.Provision of EC2 instance through Ansible 2. Retrive the public ip of instance by using concept of dynamic inventory 3. Configure the web server through Ansible

Prerequisite: 1.For installation and configuration of ansible with stable version you check this link

https://www.dhirubhai.net/pulse/integration-ansible-docker-ganesh-chaudhari

2. You need to create AWS account

https://signin.aws.amazon.com/signin?redirect_uri=https%3A%2F%2Fconsole.aws.amazon.com%2Fconsole%2Fhome%3Fstate%3DhashArgs%2523%26isauthcode%3Dtrue&client_id=arn%3Aaws%3Aiam%3A%3A015428540659%3Auser%2Fhomepage&forceMobileApp=0&code_challenge=9DSc6iRFkHWe5V2aeBUIWBlIgOwGKd8gPPDtyXhYSh8&code_challenge_method=SHA-256

Lets starts,

1. Provision of EC2 instance

First you need to login with normal user because login with root user isnot good practice. After that create ansible.cfg file for normal user in home directory and install boto and boto3 using pip3 which are used for aws connection . Write following details because aws uses ec2-user as user and uses private key with pem extension as password and ec-user is normal so we need to give root access for some tasks hence privillege escalation is created.

No alt text provided for this image

After create one Role using ansible-galaxy init server go into server/tasks/main.yml and write following YAML script and write variable like access key and secret key in server/vars/main.yml and encrypt the main.yml using ansible-vault encrypt --vault-id aws@prompt main.yml like following

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

2. Retrive public ip dynamically

For dynamic inventory pull ec2.py python script for fetching public ip of running instance . For that use wget https://raw.githubusercontent.com/ansible/ansible/stable-2.9/contrib/inventory/ec2.py then change permission chmod +x ec2.py. Modify ec2.py syntax. Then export AWS_ACCESS_KEY_ID="" and AWS_SECRET_ACCESS_KEY="" use this link for reference https://docs.ansible.com/ansible/latest/user_guide/intro_dynamic_inventory.html#inventory-script-example-aws-ec2

Use ansible all --list

No alt text provided for this image

3. Configuration of webserver

Create another Role apacheserver using ansible-galaxy init apacheserver and write apacheserver/tasks/main.yml

No alt text provided for this image

create static files in apacheserver/files like aws.conf which is configuration file of httpd and aws.html it is html page.

No alt text provided for this image

After that create two provision.yml for provision of EC2 and webserver.yml for configuration of httpd like

No alt text provided for this image

All coding done only we need to run

# ansible-playbook --vault-id aws@prompt provision.yml

Output:

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

# ansible-playbook webserver.yml

Output:

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


No alt text provided for this image

Task 2 successfully completed





Sanket Bendale

Associate Consultant at Capgemini

4 年

Great work ?

Onkar Naik

DevOps @Forescout ?? | Google Developer Expert | AWS | DevOps | 3X GCP | 1X Azure | 1X Terraform | Ansible | Kubernetes | SRE | Platform | Jenkins | Tech Blogger ??

4 年

Good work GANESH CHAUDHARI ?

Onkar Kadam

Software Engineer at NICE | Ex-Digitate | Java | Spring Boot | Microservices | DM for Referral

4 年

Well Done!!

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

Ganesh Chaudhari的更多文章

社区洞察

其他会员也浏览了