Using Ansible Dynamic Inventory to Provision EC2-Instance & Configure Apache Webserver with Handler support!

Source: LaptrinhX

Ever thought that Ansible can provision an OS with the support of configuring it dynamically. If no, then I will prove you wrong by showing the proof in this blog.

Ansible is mainly used for configuration purposes, but it also has the capability of provisioning. It is a fact that Ansible is not that much Mature Provisioning Tool as Terraform is, but yes, Ansible can do Provisioning dynamically, & to explain that process, this blog will help.

Prerequisites: AWS Account, credentials of AWS Account, & Ansible Controller Node

This content which I am publishing is very rarely available anywhere, you are in the lucky ones to get this knowledge. Happy Rare Learning!

The flow of this blog will be:

  1. Using Ansible to provision an EC2-Instance in AWS.
  2. Configuring Ansible Dynamic Inventory.
  3. Running Ansible-Playbook to execute Ansible-Role for Apache Webserver.

Using Ansible to provision an EC2-Instance in AWS!

Code to provision an EC2 Instance using Ansible — Playbook!

Make sure, you attach that security group which has the rules configured according to the requirement.

Link to the Actual GitHub Repo for this ansible Provisioning:

Configuring Ansible Dynamic Inventory!

Follow the below-mentioned steps:

  1. Go to Ansible Inventory which most probably will be located at /etc/ansible/ansible.cfg
  2. Update the path of inventory there, & make sure to enter the path of a folder(inventory folder) rather than a specific file, because there are some other files which have to be kept in the same directory for the dynamic inventory purpose. For Example, inventory = /myInventory
  3. Download both the files present in the below link & save them to your inventory folder.
  4. In the ec2.ini file, in the end, there are 2 lines which are commented out, they are asking access and secret key, add your credentials there & uncomment them.
  5. In the ec2.py file, in the first line, update your python Path.

Link to download the files:

Running Ansible-Playbook to execute Ansible-Role for Apache Webserver!

For this step, first of all, add the external roles path to the inventory where all the roles are stored. By default, Ansible read roles from 3 paths which are quite complicated, so it is suggested to add your custom path to the Inventory & save all the roles in that path only.

To add roles path, go to inventory file, & write “roles_path = <Path>”, & save it.

Now for the Webserver Role Creation, the basic code of Ansible has been used. First of all, create an empty Role Directory structure by running the command “ansible-galaxy init <RoleName>”. After that, in the specific files present in their corresponding folders add the code.

Code for all the Tasks

Handler Code

Variables

These variables you can customize according to your likes & dislikes, but make sure to update them in the main file.

Configuration File which has to be Copied to the Webserver

Make sure, to keep the same variable names everywhere to avoid problems.

This file has to be created inside the Template Directory of the Role.

Now, the setup is done. You have to now just create one small & sweet Ansible Playbook to run the Role.

Example:

In the above code just add your role name and run it with the command shown below, it will automatically fetch the IP of your ec2-instance & will configure everything there.

ansible-playbook <Name of the Playbook which will run the role> --user <name of the user which is be default created in the AWS AMI Image> --become --become-user root --private-key <path to your Private Key attached to the AWS Instance>

The aforementioned command will execute the complete setup of the Ansible.

Link to my GitHub Repo for the Ansible Webserver Role:

Link to my GitHub Repo of Ansible Complete:

I hope my article explains each and everything related to the topic with all the deep concepts and explanations. Thank you so much for investing your time in reading my blog & boosting your knowledge!

--

--

--

Big Data Enthusiast, have a demonstrated history of delivering large and complex projects. Interested in working in the field of AI and Data Science.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Trumpworld’s artificial scholarly con artists are helping him degrade popular government with…

Getting Your Head Around Story Points

Migrating Terraform State from Azure Blob Storage backend to Gitlab backend in a CI Pipeline

Assignment 3: Augmented Reality & Scroll Bars

How to Install the Jupyter Notebook Server in WSL2

It’s hard to become a developer

Top 7 Use Cases for Web Automation in Telecommunications

Jenkins Industry Use Cases

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Harshit Dawar

Harshit Dawar

Big Data Enthusiast, have a demonstrated history of delivering large and complex projects. Interested in working in the field of AI and Data Science.

More from Medium

AWS Instance Metadata Service: A Quick Refresher

A Gentle Introduction to Terraform. (Part 1)

AWS S3 Encryption

Using AWS CLI to create an EC2 instance with an Apache Webserver