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 Sources: These sources define the location and format of your host data. Ansible Controller supports various sources like cloud providers (AWS EC2, GCP), virtualization platforms (VMware vCenter), and configuration management tools (Red Hat Satellite 6).
- Inventory Plugins: These plugins parse the data retrieved from the inventory source and translate it into a format usable by Ansible.
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:
- Amazon Web Services EC2
- Google Compute Engine
- Microsoft Azure Resource Manager
- VMware vCenter
- Red Hat Satellite 6
- Red Hat Insights
- OpenStack
- Red Hat Virtualization
- Red Hat Ansible Automation Platform
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:
- Create an
ini
orYAML
file, for example,demohosts.ini
, containing your host definitions. You can find a sample inventory file here. - This file can hold information for multiple environments (e.g., PRD, DEV, UAT) or a single inventory, depending on your needs.
[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.
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.
- Verify that the “Groups” and “Hosts” sections are empty for a new inventory.
4. Add Inventory Source:
- Go to the “Sources” tab within the inventory.
- Click “Add” to create a new inventory source.
- Configure the source details:
- Source: “Sourced from Project”
- Project: Select the project created in Step 2 (e.g., “Project for inventory source”).
- Inventory file: Specify the inventory file created in Step 1 (e.g.,
demohosts.ini
). - Update options:
- Overwrite: Checked: Removes hosts/groups no longer present in the inventory file. Unchecked: Local changes remain untouched.
5. Sync and Verify:
- Save the source configuration.
- Click “Sync” to retrieve information from your inventory file.
- Check the “Hosts” and “Groups” sections in the inventory. You should see the information populated based on your
demohosts.ini
file.
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.
Updating Your Inventory:
- Simply modify your inventory file (
demohosts.ini
) in the source (Git) and perform the following actions:- Sync the source project in Ansible Controller.
- Sync the inventory source.
Automation Options:
Ansible Controller offers automation features to streamline updates:
- Updated on launch: This option, when enabled for an inventory source, refreshes the inventory from the source before each job execution.
- Update Revision on Launch: (Project for inventory source) This ensures the project itself is synced before refreshing the inventory source.
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: