Deploy Web Server On AWS Through Ansible
In this article, we will be launching EC2 instance on AWS using Ansible and after launching we will be configuring webserver in ec2 instance.
What is Ansible?🤔
Ansible is an open-source automation tool, or platform, used for IT tasks such as configuration management, application deployment, intraservice orchestration, and provisioning. Automation is crucial these days, with IT environments that are too complex and often need to scale too quickly for system administrators and developers to keep up if they had to do everything manually. Automation simplifies complex tasks, not just making developers’ jobs more manageable but allowing them to focus attention on other tasks that add value to an organization. In other words, it frees up time and increases efficiency. And Ansible, as noted above, is rapidly rising to the top in the world of automation tools.
What we are going to do throughout this article?
🌎Provision EC2 instance through ansible.
🌎Retrieve the IP Address of instance using a dynamic inventory concept.
🌎Configure the webserver through ansible!
🌎Create a role for the webserver to customize the Instance and deploy the webpage to the root directory.
There is some prerequisite that is to be fulfilled before this task:
Ansible should be preinstalled in your system and you should have an AWS account.
👉🏼STEP-1
We will define the localhost IP address in the host file as this will behave like a managed node.
👉🏼STEP — 2
We will create an Ansible vault to store our AWS user credientials like access key and secret key that we get at the time of user creation in the AWS account.
✏️Ansible Vault is a feature of Ansible that helps to store sensitive data like passwords in the encrypted file.
To create an ansible vault file command is:
ansible-vault create --vault-id Abhi@prompt secret.yml
This command will prompt and ask the user to enter the vault password secret.yml is the file name.
Inside this file, we will create two variables- access_key and secret_key.
As we have used Ansible Vault so this file is encrypted and no one can see data inside this file.
If we try to see the data inside the file it will look like this-
👉🏼STEP — 3
Deployment file that will launch EC2 instance on AWS will be created.
In this file, we will define all the variable that is required to launch EC2 instance like key_name, instance_type, image id, subnet id, etc.
playbook
- hosts: "localhost"
vars_files:
- secret.yml
tasks:
- name: "ANSIBLE_AWS_EC2"
ec2:
key_name: "Abhinav"
instance_type: "t2.micro"
image: "ami-0ebc1ac48dfd14136"
wait: yes
count: 1
instance_tags:
Name: EC2_BY_ANSIBLE
vpc_subnet_id: "subnet-3e0b1456"
assign_public_ip: yes
region: "ap-south-1"
state: present
group_id: "sg-452b0f3c"
aws_access_key: "{{access_Key}}"
aws_secret_key: "{{secret_Key}}"
register: Abhi
- debug:
var: Abhi.instances[0].private_ip
register: IP_Var
- debug:
var: IP_Var["Abhi.instances[0].private_ip"]
After writing the above file we will run the file by using the command
ansible-palybook --vault-id Abhi@prompt Aws.yml
Here AWS.yml is a file name.
Our EC2 has been launched successfully.
The above file will also retrieve the private IP of the launched EC2 instance.
Now, this Ip we will define in our Inventory file for further working inside Ec2 instance. Also, we will be defining user name and location of key which will help into login into the instance.
👉🏼Step-4
Now, we will configure HTTPD server in the Ec2 instance. For this we will write one playbook.
- hosts: all
tasks:
- name: httpd ec2
command: "sudo yum install httpd -y"- name: start service
service:
name: "httpd"
state: started
enabled: yes- name: copy files
copy:
src: "/etc/ansible/Task_1/abhinav.html"
dest: "/var/www/html/"
This playbook will install HTTPD server and copy the file in the destination folder.
Here, I have created an basic HTML file named as abhinav.html
Now, we will be running this file.
ansible-playbook web.yml
It’s done! now by using public IP of the launched instance we can see the output.