Launching EC2 instances and configuring the Web Server using dynamic inventory method from Ansible on AWS.
In this article we will see:-
- Launch EC2 instances on AWS using Ansible.
- Retrieve the public IP address allocated to the launched instance.
- With the help of the retrieved Public IPv4 address configure the web server in the launched instance.
- 4. Create a role for the webserver to customize the Instance and deploy the webpage.
AWS is a cloud service provided by Amazon. Amazon Elastic Compute Cloud (Amazon EC2 ) is a part of Amazon’s cloud-computing platform that allows users to rent virtual computers on which to run their own computer applications. AWS EC2 service provisions resources like RAM , Hard Disk ,CPU etc.
Ansible is an open-source software provisioning, configuration management, and application-deployment tool enabling infrastructure as code. The code written is extremely simple and to the point and we only mention our requirements and it does everything by itself.
Prerequisite
- Have AWS account.
- Created IAM role.
- Ansible installed.
Step 1:- Installing “boto” & “boto3” libraries to interact ansible with AWS
Step 2:- I have stored the AnsibleTesting key in pem format and also coverted it to ppk format named ansibleppkk and stored it in the home directory.
3.Make the necessary changes in the ansible configuration file.
4.Now for Dynamic Inventory ,we need to download two files -ec2.py and ec2.ini from Github. For this we make a directory mydb and download it there.
mkdir mydb
Now , in this directory using wget command download two files ec2.py and ec2.ini -
wget https://raw.githubusercontent.com/ansible/ansible/stable-2.9/contrib/inventory/ec2.py
wget https://raw.githubusercontent.com/ansible/ansible/stable-2.9/contrib/inventory/ec2.ini
Inside ec2.py , Change this two lines -
Change:-1. #!/usr/bin/env python to #!/usr/bin/python3
2. Comment the line 172 out
Inside ec2.ini file:-
Make sure to include the aws secret key and aws access key in the last line.
5.Inside myroles directory create aws_roles folder. Here we create ec22.yml file:-
Note:- Here I have directly put the access and secret keys which are extremely secure pieces of information. It is advisable to not use them directly like I did. Instead they should be stored in variables and locked in vaults. However to reduce effort I have used them like this.
6.Now I check if I am able to list and ping all the hosts.
ansible all--list-hosts --> will give IP of hosts available on AWS
ansible all-m ping --> will check weather its pinging or not
7.Now I run the main playbook file which is ec22.yml
In the playbook since I have mentioned ‘name’ inside the instance_tags and set the count as 3, it has launched 3 instances in my AWS account.
8.Now we make a directory task3:-
mkdir /etc/myroles/task3
Here I create a yml file named task2.yml and edit it accordingly. We can select any of the IP address of the 4 instances launched.
Make sure we have an html file called ‘task2.html’ inside the same location.
9. Finally we can see the task2.html launched.
Henceforth the task is completed.