Ansible Dynamic Inventory: When and How to Use It

In our rapidly evolving world of technology, automation has become a sine qua non for IT departments. And when we talk about IT automation, Ansible is a name that immediately comes to mind owing to its power and simplicity. It provides capabilities ranging from simple task automation to complete infrastructure orchestration. One feature that stands out is the Ansible dynamic inventory. But what is it, and when should you use it? Let’s demystify that today. 

What Is a Dynamic Inventory in Ansible?

As the name suggests, a dynamic inventory is the live, real-time version of your infrastructure inventory. It contrasts with the traditional static inventory, which, as the name indicates, is static and manually updated. 

Static inventory was Ansible’s starting point. It required a manual inventory file detailing all the hosts to be managed. The problem? It wasn’t designed to deal with the rapid pace of change in modern cloud environments. Keeping track of new machines being created or old ones being decommissioned was a logistical nightmare. 

Enter the dynamic inventory. It allows Ansible to fetch a real-time list of hosts from external systems, and it does this by running a script or a program. So instead of a hard-coded list of hosts, you can use an executable file that generates the inventory list. This saves you from having to manually track the changes. 

A static inventory should suffice if your environment is small and stable, where the nodes don’t change often. However, a dynamic inventory is a much better fit if you have a large and dynamic environment, which is common in cloud-based infrastructures. With dynamic inventory, you can make sure that your playbook is always running on the most up-to-date list of nodes. 

How to Start Using Dynamic Inventory

Now that we’ve established the what and when, it’s time to tackle the how. Let’s discover how you can start leveraging dynamic inventory in your Ansible workflows. 

What Can Dynamic Inventory Be Written In?

You can write your dynamic inventory script in any language that Ansible supports. This includes PythonShell scripts, or even a compiled language like Go or Java, as long as it outputs a JSON format that Ansible can parse. 

The script must return a JSON object representing your hosts and groups. The basic structure of the JSON object should include at least one group and the hosts within that group. 

Now let’s delve into the process of configuring Ansible to utilize dynamic inventory. 

Configuring Ansible to Use Dynamic Inventory

There are two primary steps to configuring Ansible to use dynamic inventory: writing the script and instructing Ansible to use it. Let’s expand on each of these steps: 

1. Writing the dynamic inventory script

This script is a program that fetches your inventory details dynamically. You can write this program in any language that can output JSON, such as Python or Bash. This is how Ansible expects the inventory details to be presented. The script, when executed, should provide a JSON-formatted output that includes at least one group and the hosts within that group. You can also define host variables and group variables in your JSON output. These variables can then be used in your playbooks to customize the tasks based on the host or group. 

2. Instructing Ansible to use the dynamic inventory script

Once you’ve written your dynamic inventory script, the next step is to tell Ansible to use it. You can do this in a couple of different ways: 

  • Command line: When running an Ansible playbook, you can specify your dynamic inventory script directly on the command line. Here’s an example: ansible-playbook -i /path/to/your/ your_playbook.yml. The -i option tells Ansible what inventory to use. By providing the path to your script, you’re instructing Ansible to use your dynamic inventory.
  • Ansible configuration (Ansible.cfg): Alternatively, you can specify your dynamic inventory script in the Ansible configuration file. The default configuration file is located at /etc/ansible/ansible.cfg, but you can also have a configuration file in your current directory. This file contains a defaults section where you can specify the path to your dynamic inventory script like this: inventory = /path/to/your/ When you run an Ansible playbook, it uses this configuration file to determine what inventory to use.

Moreover, Ansible also allows you to mix static and dynamic inventories. For example, you might have a few static hosts while the rest are dynamic. Ansible lets you specify multiple inventories and will merge them for you. You can do this by listing multiple inventories on your command line or in your Ansible configuration file. 

Finally, remember that your script must be executable, and it must have the appropriate shebang line at the top to indicate its interpreter (e.g., #!/usr/bin/env python3 for a Python script). 

Once you’ve configured Ansible to use your dynamic inventory script, you’re ready to run your playbooks. With each run, Ansible will execute your script, gather the current state of your hosts, and use that to execute your playbook. This makes Ansible an incredibly powerful tool for managing large, dynamic environments. 

Using NetBox as Your Source of Truth

NetBox is a robust IPAM and DCIM solution that can serve as an excellent source of truth for your infrastructure. It allows you to maintain a database of your physical and virtual network devices, IP addresses, and subnets among other things. 

To leverage NetBox as a source for your Ansible dynamic inventory, you can use the Ansible NetBox dynamic inventory plugin. This plugin is a prewritten Python script that communicates with the NetBox API to pull in the latest data. It automatically formats this data into the JSON structure that Ansible expects, reducing the amount of manual work required to write your script. 

Here are the steps to integrate NetBox with Ansible: 

  1. Install the NetBox plugin: You can install the plugin using the Ansible Galaxy command: ansible-galaxy collection install netbox.netbox.
  2. Configure the plugin: You must create a configuration file for the plugin. In this file, you specify your NetBox URL and API token, and you can set additional parameters to customize what data the plugin pulls from NetBox.
  3. Run your playbook: Now you can run your playbook with the dynamic inventory from NetBox. Use the ‘-i’ option to specify your plugin configuration file.

By using NetBox as the source of truth for your dynamic inventory, you can make sure that your Ansible playbook is always running against the most accurate snapshot of your infrastructure. This significantly reduces the risk of errors due to outdated information. 

In Conclusion

The Ansible dynamic inventory is a powerful tool for managing complex, ever-changing environments. It eliminates the need for manual inventory updates and ensures your playbooks run on the latest infrastructure data. By integrating NetBox, you further streamline the process, making your infrastructure management dynamic. 

Combining Ansible’s simplicity and power with NetBox’s robust data management capabilities offers an unparalleled solution for modern infrastructure management. Whether running a small on-premise network or managing a large-scale cloud infrastructure, Ansible and NetBox can help you automate your operations and manage your resources more efficiently. 

This post was written by Juan Reyes. As an entrepreneur, skilled engineer, and mental health champion, Juan pursues sustainable self-growth, embodying leadership, wit, and passion. With over 15 years of experience in the tech industry, Juan has had the opportunity to work with some of the most prominent players in mobile development, web development, and e-commerce in Japan and the US.

Share the Post:

Related Posts