What is Dynamic Inventory in Ansible?

Ansible is a tool that helps automate IT tasks like configuring servers, deploying software, or managing environments. Instead of manually logging into each server to make changes you can write simple "playbooks" and Ansible will automatically apply those changes to your servers.

What is an Inventory in Ansible?

In Ansible, an inventory is a list of servers (also called hosts) that you want to manage. Normally, you'd define these servers in a text file (called a static inventory) where you manually write down their details, like IP addresses or hostnames

For example a static inventory file might look like this:

[webservers]
server1.example.com
server2.example.com

In this example, webservers is a group, and it contains two servers: server1.example.com and server2.example.com.

What is Dynamic Inventory?

Now imagine you’re using a cloud service (like AWS, Google Cloud, or Azure) where servers are created and destroyed frequently. Manually updating the inventory file every time a server is added or removed would be really hard to manage.

That's where dynamic inventory comes in.

With dynamic inventory, instead of writing out every server manually, Ansible automatically discovers your servers by pulling information from your cloud provider or other external systems in real time.

How Does Dynamic Inventory Work?

  1. External Source: Ansible can query external services, like AWS, Google Cloud, or even a database, to find out what servers (hosts) exist at the moment.

  2. Script or Plugin: Ansible uses a script or a plugin to ask these services for the list of available servers.

  3. Automatic Update: Instead of updating your inventory file manually, Ansible gets this list automatically and keeps it updated.

Example:

Let’s say you are using AWS to host your servers. With dynamic inventory Ansible can automatically get the list of your EC2 instances without you needing to write their IPs in the inventory file.

When you run an Ansible playbook it will reach out to AWS (or whatever cloud provider you are using) and it will fetch the list of active servers at that time. If you add or remove servers in AWS Ansible will know about the changes without needing you to update anything manually.

Why Use Dynamic Inventory?

Simple Example of Using Dynamic Inventory:

If you use AWS Ansible has a built-in plugin that can connect to AWS and pull a list of EC2 instances. You just configure your AWS credentials and Ansible will handle the rest.

Copy

ansible-inventory -i aws_ec2.yml --list

This will give you a list of your EC2 instances directly from AWS. No need to manage a static file yourself.

Final Word:

Dynamic inventory helps automate the process of finding and managing your servers, especially when using cloud or other dynamic environments. Instead of manually writing down the list of servers Ansible does it for you and keeps it updated automatically!

0
Subscribe to my newsletter

Read articles from Giri Naresh Allu directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Giri Naresh Allu
Giri Naresh Allu

specializing in the development of scalable, efficient and hybrid platforms. My expertise centres around the advanced use of various Integration technologies, container orchestration, and automation to enhance operations and support continuous integration and deployment. Known for implementing key initiatives, modernize, migrate tech stack and implement state of the art frameworks in the organization. I am keen to apply my extensive skills and innovative methods to further the success of the organization.