Site icon techbeatly

Dynamic Inventory in Ansible Controller: Use an Inventory File as a Source

Ansible’s inventory plays a crucial role in defining the infrastructure you manage. It holds information about your hosts, such as their names, variables, and IP addresses. Traditionally, this information was stored in static inventory files. But what if your infrastructure is constantly changing? Dynamic inventory offers a powerful solution.

Understanding Inventory Sources and Dynamic Updates

Dynamic inventory eliminates the need for manual updates by fetching host information from various sources in real time. This ensures your Ansible playbooks always target the correct hosts, regardless of infrastructure changes.

Ansible Controller leverages two key concepts for dynamic inventory:

Inventory Plugins Rewritten

Previously, inventory plugins required separate configuration files. However, in the Ansible automation controller, you can directly provide the new-style inventory plugin configuration through the source_vars field for supported sources. This simplifies the process for managing inventory plugins associated with:

Let’s Get Practical: Dynamic Inventory with Sourced from Project

Here’s a step-by-step guide on creating dynamic inventory using the “Sourced from Project” option in Ansible Controller:

1. Prepare Inventory File:

[demohosts]
node1sample
node2sample
node10sample mycutom_var=testing port=8443

[demodb]
dbsample1
dbsample2
dbsample3
dbsample4

[network]
cisco101 mycutom_var=prod
cisco102 env=dev

[web]
webserver[101:102] http_app=nginx
webserver[201:202]

[web:vars]
web_http_port = 8080

[demodb:vars]
db_port = 5432

2. Create a Project with Repository:

It’s recommended to store your inventory file in a separate Git repository for version control and as a single source of truth. Create a project within Ansible Controller that links to this repository.

Create a new project in the automation controller with the Git repo where we have the inventory file.

3. Create/Configure Inventory:

Navigate to the Inventories section and create a new inventory or use an existing one. Adjust configurations as needed, but primarily focus on the inventory name.

Create new inventory in the automation controller.
Empty hosts list in the new inventory.
Empty groups list in the new inventory.

4. Add Inventory Source:

Create inventory source

5. Sync and Verify:

Sync the inventory source
Hosts synced from the inventory source.
Groups synced from the inventory source.

You can also check the variables you set for hosts or groups as follows.

We have node10sample mycutom_var=testing port=8443 in our inventory file (demohosts.ini) and see the same synced on the automation controller.

Host variables synced from the inventory source.

Updating Your Inventory:

Automation Options:

Ansible Controller offers automation features to streamline updates:

By implementing dynamic inventory using “Sourced from Project,” you gain a flexible and automated approach to managing your Ansible infrastructure. Remember to adapt the configurations and best practices to suit your specific needs.

References:

Exit mobile version