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
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.
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
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
3. Configuration of webserver
Create another Role apacheserver using ansible-galaxy init apacheserver and write apacheserver/tasks/main.yml
create static files in apacheserver/files like aws.conf which is configuration file of httpd and aws.html it is html page.
After that create two provision.yml for provision of EC2 and webserver.yml for configuration of httpd like
All coding done only we need to run
# ansible-playbook --vault-id aws@prompt provision.yml
Output:
# ansible-playbook webserver.yml
Output:
Associate Consultant at Capgemini
4 年Great work ?
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 ?
Software Engineer at NICE | Ex-Digitate | Java | Spring Boot | Microservices | DM for Referral
4 年Well Done!!