Working with Ansible Dynamic Inventory using AWS

Search for a command to run...

No comments yet. Be the first to comment.
The finished product of this project is terraform code that builds an AWS environment with three EC2 instances. The Jenkins CI/CD Pipeline will be triggered when the code is pushed to a GitHub repository via GitHub Webhook. What is Jenkins? Jenkins i...

In this article, you will learn how to use Ingress-NGINX Controller for Kubernetes to route traffic to Grafana and Prometheus respectively. What is an Ingress? In Kubernetes, an Ingress is an API object that manages external access to services within...

In this article, you will learn how to create a Jenkins AMI using Hashicorp Packer and Ansible Playbook. What is Packer? Packer is an open-source tool for creating identical machine images for multiple platforms from a single source configuration. Pa...

Protecting your AWS account access keys is critical for protecting your company's cloud infrastructure. Managing several accounts, on the other hand, can be complicated and time-consuming. aws-vault secures the storage and management of access keys f...

Maintaining the inventory file while working with Ansible with AWS can be a difficult process owing to frequent changes in dynamic components of the infrastructure.
Fortunately, there is a convenient solution called Ansible Dynamic Inventory. Dynamic inventory is an Ansible plugin that utilizes API calls to AWS to retrieve instance information during runtime. This approach provides you with the ability to manage the AWS infrastructure dynamically by obtaining up-to-date details of EC2 instances.
By leveraging dynamic inventory, you can automate the process of discovering and managing your AWS resources without the need for manually updating the inventory file. Instead, Ansible interacts directly with the AWS API to obtain the necessary information, allowing for more flexibility and ease of management.
In this tutorial, you will learn how to configure dynamic inventory on AWS using the AWS ec2 Ansible plugin.
Ansible Installed
Python3, Pip3 and Boto3
AWS Account
SSH key pair
EC2 Instances (preferably 2 or more) tagged
Ansible generates an inventory file, which is usually stored in /etc/ansible/hosts. During a playbook or command execution, Ansible uses this as the default location if a custom inventory file is not specified with the -i option.
To get started, access your home folder and create a new directory to hold your Ansible files:
cd ~
mkdir ansible
vim:cd ansible
vim aws_ec2.yaml
plugin: amazon.aws.aws_ec2
# The access key for your AWS account.
aws_access_key: <YOUR-AWS-ACCESS-KEY-HERE>
# The secret access key for your AWS account.
aws_secret_key: <YOUR-AWS-SECRET-KEY-HERE>
regions:
- us-east-1
filters:
instance-state-name: running
keyed_groups:
- key: tags
prefix: tag
- prefix: instance_type
key: instance_type
- key: placement.region
prefix: aws_region
- prefix: az
key: placement.availability_zone
Warning: Do not commit this file to a public git repository
For ENV:
export AWS_ACCESS_KEY_ID='YOUR_AWS_ACCESS_KEY_ID'
export AWS_SECRET_ACCESS_KEY='YOUR_AWS_SECRET_ACCESS_KEY'
Ansible.cfg filesudo vim /etc/ansible/ansible.cfg
[defaults] section of the file and change the inventory parameter valueinventory = /home/<USERNAME>/ansible/aws_ec2.yaml

[inventory] section of the file and change the plugin's parameter value. The following line is to enable the ec2 plugin.enable_plugins = aws_ec2

Execute the following command to see the listings of the ec2 instances.
ansible-inventory --list

Tags, instance types, instance names, custom filters, and other criteria can be used to organize instances. View all supported filters and keyed groups from this page.
The example below of keyed_groups:
keyed_groups:
- key: tags
prefix: tag
- prefix: instance_type
key: instance_type
- key: placement.region
prefix: aws_region
- prefix: az
key: placement.availability_zone
Execute the following command to list the ec2 dynamic inventory groups.
ansible-inventory --graph
Instances will be grouped under the tags aws_ec2, aws_region_us_east_1, az_us_east_1d , instanace_type_t2_micro and tag_Name_ubuntu_server .

If I want to execute an Ansible playbook on all of the servers tagged ubuntu_server. The sample playbook is shown below. It uses the apt module to install Nginx on all the Ubuntu servers.
Create an ansible-playbook named ec2-playbook.yaml .
For Debian and Ubuntu instances use:
- name: Playbook Example
gather_facts: false
hosts: tag_Name_ubuntu_server
tasks:
- name: Install the package Nginx
ansible.builtin.apt:
name: nginx
state: present
For CentOS, Amazon Linux2 and Amazon Linux instances use:
- name: Playbook Example
gather_facts: false
hosts: tag_Name_ubuntu_server
tasks:
- name: Install the package Nginx
ansible.builtin.yum:
name: nginx
state: present
Execute the playbook with the following command:
ansible-playbook ec2-playbook.yaml -u <ec2-instance-username> –-private-key=<keyfilename.pem>
Get the default username for the AMI that you used to launch your instance:
| AMI used to launch an instance | Default username |
| Amazon Linux2 and Amazon Linux | ec2-user |
| CentOS | centos or ec2-user |
| Debian | admin |
| Ubuntu | ubuntu |
The image below shows Nginx has been successfully installed on the Ubuntu servers.
