Deploying Webserver on Aws through Ansible!
4 min readOct 7, 2020
Task Details:
$ Provision EC2 instance through ansible
$ Retrieve the IP Address os instances using dynamic inventory concept
$ Configure the webserver through ansible
$ create a role for the webserver to customize the instance and deploy the webpage to root directory
Let's begin our task
- To contact ansible to was cloud we have to install boto and boto3
pip3 install botopip3 install boto3
- Let's create a role for ec2 instance provisioning
ROLE:
Roles let you automatically load related vars_files, tasks, handlers, and other Ansible artifacts based on a known file structure. Once you group your content in roles, you can easily reuse them and share them with other users.
To create a role
ansible-galaxy init rolename
- Now we have to write yml file for launching the ec2 instance
- I have written variables in other file
- For as credentials I have created a vault
- Ansible Vault encrypts variables and files so you can protect sensitive content such as passwords or keys rather than leaving it visible as plaintext in playbooks or roles
ansible-vault create --vault-id aws@prompt credentials.yml
- Now create a playbook for our role
- Now we will run our playbook for launching the instance
ansible-playbook --vault-id aws@prompt playbookname.yml
- Now let's check-in AWS console
- We have launched the instance, now we have to set up dynamic inventory
- For a dynamic inventory, we have to download the dynamic inventory scripts from Github.
- These scripts are in python because ansible is built upon python
https://raw.githubusercontent.com/ansible/ansible/stable-2.9/contrib/inventory
- From this URL, we have to download the two files named as ec2.py and ec2.ini
- I have used wget command for downloading the scripts
- After downloading the scripts, in ec2.py first line write python3
- Now make the files executable chmod +x ec2.py , ec2.ini
- Now we have to export our aws key
export AWS_REGION=''
export AWS_ACCESS_KEY_ID=''
export AWS_SECRET_ACCESS_KEY=''
- If we do everything correct we will see the instance IP
ansible all --list-hosts
- Now create a role for webserver setup to our instance
- Now write yml file for webserver
- We have to change the permission of pem key by chmod 400
- We have to write in dynamic inventory path in ansible configuration file
- Ansible will log in to the ec2 instance using the ec2-user username.
- In order to install required packages, it needs permissions and so, we also have to provide the privilege escalation to the user using sudo.
- Now write a playbook for webserver
- Run the playbook
- Final output :
lets check in google whether our task finished !
give instance IP in google search engine
Thanks for reading!
Thank you vimal daga sir!