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.
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.
- hosts: "localhost"
- name: "ANSIBLE_AWS_EC2"
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.
Now, we will configure HTTPD server in the Ec2 instance. For this we will write one playbook.
- hosts: all
- name: httpd ec2
command: "sudo yum install httpd -y"- name: start service
enabled: yes- name: copy files
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.
It’s done! now by using public IP of the launched instance we can see the output.