Ansible and AWS

Deploy Web-Server on AWS through ANSIBLE!

Priyanka Bhide
4 min readMar 7, 2021

--

What is ANSIBLE?

Ansible is an open-source software provisioning, configuration management, and application-deployment tool enabling infrastructure as code.

What is an AWS EC2 Instance?

An EC2 instance is a virtual server in Amazon’s Elastic Compute Cloud for running applications on the Amazon Web Services infrastructure.

🔰 Task — Deploy Webserver on AWS through Ansible!

♦️ Provision EC2 instance through ansible.

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

♦️ Configure the webserver through ansible!

♦️ Create a role for the webserver to customize the Instance and deploy the webpage.

Let’s start the task …

Step 1: First of all, we need to install python package called boto , before launching the instance , as it provides programmatic connectivity to AWS.

command : pip3 install boto

Step 2 : Create a role for launching the AWS instance.

command : ansible-galaxy role init aws-instance(role _name)

  • Write the code inside tasks/main.yml file of roles , for launching AWS instance.
  • Write the value of access and secret variables inside the vars/main.yml file, which contains AWS_Access_Key and AWS_Secret_Key.
   access: XXXXXXXXXXX
secret: XXXXXXXXXXX
  • Give the path of role inside the ansible configuration file. i.e./etc/ansible/ansible.cfg

Step 3 : Create a playkbook that will connect aws-instance role to launch the instance.

  • Run the playbook in localhost as we don't have public IP of the instance.

command : ansible-playbook aws.yml

The instance has been launched Successfully in AWS …👇

Step 4 : To get the Public IP of instance dynamically in the inventory Download the ec2.py and ec2.ini files.

command : wget https://raw.githubusercontent.com/ansible/ansible/stable-2.9/contrib/inventory/ec2.py

command : wget https://raw.githubusercontent.com/ansible/ansible/stable-2.9/contrib/inventory/ec2.ini

  • Make both file executable file executable.
  • Inside ec2.py file , change the firstline 👇 to execute the file with proper python interpreter.
#!/usr/bin/python3
  • Provide the Access Credentials i.e. AWS Access Key and Secret Key.

Step 5 : Now , Configure the inventory .

  • Now , We can get the public IP of the instance and it will be able to ping from any source.

command : ansible all — list-hosts

Let’s Configure the Webserver …

Step 6 : Create a role for Configuring webserver inside the EC2 instance.

command : ansible-galaxy role init webserver

Step 7 : Write the code for webserver Configuration in tasks/main.yml file of webserver role.

Step 8 : Create a playkbook that will connect webserver role to configure it.

Now , Run the playbook .

command : ansible-playbook web.yml

As you can see below , Your Webpage has been deployed successfully over the AWS cloud.

Task Completed !!!

Thank you .

--

--