LAUNCHING EC2 INSTANCE AND CONFIGURING WEBSERVER IN MANAGED NODE BY DYNAMIC INVENTORY USING ANSIBLE ON AWS

Abhishek Sharma
4 min readOct 23, 2020

--

Deploy Web Server on AWS through ANSIBLE!

♦️ Provision EC2 instance through ansible.

♦️ Retrieve the IP Address of instance using dynamic inventory concept.

♦️ Configure the web server through ansible!

♦️ Create role for web server to customize the Instance and deploy the webpage to root directory.

Optional : Write a playbook for testing of the tasks.

For Example: If the task is installation of httpd package then for checking the status of package, ansible has the module named as package_facts.

just like that search modules for checking status of each task! if u don’t find any module then command module can be useful

Note: Complete process have to be automated!

Prerequisites for this project:

1. AWS account should be created and, AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY are required.

2. Ansible version 2.9 should be installed.

First we have to update with update command we used dnf you used yum also both work on redhat8

sudo yum update

Then we install Python3 in redhat8 they have pre-install but check to install

sudo yum install python3

Install Pip also

sudo dnf install python3-pip

Now all set, now install Ansible

pip3 install ansible — user

Step 1. Launch ec2 instance

- hosts: localhost
gather_facts: no
vars_files:
- awspass.yml
tasks:
- name: Launching ec2 Instance
ec2:
key_name: "awskey"
instance_type: "t2.micro"
image: "ami-0ebc1ac48dfd14136"
wait: "yes"
count: 1
vpc_subnet_id: "subnet-11b3b479"
assign_public_ip: yes
region: "ap-south-1"
state: present
group_id: "sg-010258f611d46ff66"
aws_access_key: "{{ myuser }}"
aws_secret_key: "{{ mypass }}"
register: ec2
- debug:
var: ec2.instances[0].public_ipvim awspass.yml

myuser: “Paste Your ACCESSKEY here “

mypass: “Paste your SECRETKEY here”

ansible-playbook playbook_name.yml

Step 2. Updating inventory dynamically

Now, we will create a dynamic inventory that will update dynamically according to the instances present in the AWS.

For this purpose, Ansible provides us pre-created files for EC2. Just download the ec2.py and ec2.ini files form the Ansible on the GitHub branch stable-2.9 and place these files in the folder that has all of the inventory files.

wget https://raw.githubusercontent.com/ansible/ansible/stable-2.9/contrib/inventory/ec2.pywget https://raw.githubusercontent.com/ansible/ansible/stable-2.9/contrib/inventory/ec2.ini
chmod  +x ec2.py chmod  +x ec2.ini

Now, update the ec2.ini file

Also add your access key and secret access in the file

AWS_ACCESS_KEY=XXXX
AWS_SECRET_KEY=XXXX

After this export all these commands

export AWS_REGION='ap-south-1'
export AWS_ACCESS_KEY=XXXX
export AWS_SECRET_KEY=XXXX

Finally, we had successfully done the setup for updating inventory dynamically and according to our requirements.

But what is dynamic inventory in ansible ??

As described in Working with dynamic inventory, Ansible can pull inventory information from dynamic sources, including cloud sources, using the supplied inventory plugins. If the source you want is not currently covered by existing plugins, you can create your own as with any other plugin type.

After that you need to give this /mydb path location in /etc/ansible/ansible.cfg file. Now ansible will know that how many ec2 instances are running on AWS. You can check via below commands.

Step 3. Configure the web server through ansible.

What is Role?

Roles provide a framework for fully independent, or interdependent collections of variables, tasks, files, templates, and modules. In Ansible, the role is the primary mechanism for breaking a playbook into multiple files. This simplifies writing complex playbooks, and it makes them easier to reuse.

So now we are creating one role by using command:

ansible-galaxy init ec2-web

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.

- hosts: all
- roles: ec2-web

In the tasks sub-directory there is a main.yml file which contains the role’s task definition.

#task for ec2-web


tasks:
- name: "Configure webserver"
package:
name: "httpd"
state: present
- name: "Copy webpage"
copy:
dest: "{{ dcdir }}"
src: "files/home.html"
- name: Start the service
service:
name: "httpd"
state: started
enabled: yes

Now, finally run the ansible-playbook

ansible-playbook web.yml

As the code Successfully run. then we can check the webpage via. public ip of ec2 instance/home.html

And it works..!!

Thank You….

--

--

No responses yet