Using Ansible Dynamic Inventory to Provision EC2-Instance & Configure Apache Webserver with Handler support!
This blog will explain the process of provisioning EC2-Instance & configuring webserver in it using the Ansible Dynamic Inventory which is very rarely available! The code for the same has been presented & explained in this blog. Also, a complete code link has been given at the end of this blog.
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:
- Using Ansible to provision an EC2-Instance in AWS.
- Configuring Ansible Dynamic Inventory.
- Running Ansible-Playbook to execute Ansible-Role for Apache Webserver.
Using Ansible to provision an EC2-Instance in AWS!
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:
This repository contains multiple Ansible Playbooks which can be used to multiple real-world use-cases! …
Configuring Ansible Dynamic Inventory!
Follow the below-mentioned steps:
- Go to Ansible Inventory which most probably will be located at /etc/ansible/ansible.cfg
- 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
- Download both the files present in the below link & save them to your inventory folder.
- 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.
- In the ec2.py file, in the first line, update your python Path.
Link to download the files:
You can't perform that action at this time. You signed in with another tab or window. You signed out in another tab or…
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
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.
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:
A brief description of the role goes here. Any pre-requisites that may not be covered by Ansible itself or the 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!