Deploy Web Server on AWS through Ansible
First let’s discuss about Ansible :
Ansible is an open source automation platform. It is a simple automation language or tool. It can configure systems, deploy software, and orchestrate more advanced IT tasks such as continuous deployments or zero downtime rolling updates.
Task Description :
- 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.
Step 1: Provison EC2 instance through ansible.
For provisioning EC2 instance through ansible we can use ec2 module. This module helps us to create, terminate, start or stop an instance in ec2.
Ansible Playbook for provisioning EC2 instance :
Here I have created vault file for access key and security key with variables accessKey and secretKey. This playbook will provision an ec2 instance on aws cloud.
Here is the ec2 instance which is launch by ansible.
Step 2 : Retrieve the IP Address of instance using dynamic inventory concept.
An inventory defines a collection of hosts that Ansible will manage. Host inventories can be defined in two ways. A static host inventory can be defined by a text file and a dynamic host inventory can be generated by a scripts or other program.
Here I am using a python program for generating dynamic host inventory. For this I downloaded https://github.com/ansible/ansible/blob/stable-2.9/contrib/inventory/ec2.py file and after that I made this file executable and also set all the required environmental variable.
Now we need to update ansible configuration file so that inventory can be dynamically updated.
Here we have provide remote user and private key to ansible configuration file because ansible will require this to do ssh into the ec2 instance. Also I have set up the [privilege_escalation] so that when ansible connect to ec2 instance with ec2-user it can get administrative access as root.
Step 3 : Configure the web server through ansible.
For configuring the webserver through ansible I am using the concept of ansible role which provide a way for managing our projects and also makes our code easier to reuse.
We can use command to create role :
ansible-galaxy init rc2-web
In the tasks sub-directory there is a main.yml file which contains the role’s task definition.
In the vars sub-directory there is a main.yml file which contains the role’s variable value.
In the files sub-directory there is a static file of the website which is referenced by the role tasks.
Now I have created a web.yml file to call the role in a playbook, and after running the playbook it will configure a web server on ec2 instance.
Here is the final output of the complete task :
Thank You !!