Get up to 50% off on CKA, CKAD, CKS, KCNA, KCSA exams and courses!

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

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 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 or YAML 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
  1. 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.
- Verify that the “Groups” and “Hosts” sections are empty for a new inventory.
Empty hosts list in the new inventory.
Empty groups list in the 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.
Create inventory source
5. Sync and Verify:
  • Save the source configuration.
  • Click “ Sync ” to retrieve information from your inventory file.
Sync the inventory source
- Check the “Hosts” and “Groups” sections in the inventory. You should see the information populated based on your `demohosts.ini` file.
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:
  • 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:

  • Sourced-from-a-project
Share :

Related Posts

Customizing Ansible: Creating Execution Environment Images

Customizing Ansible: Creating Execution Environment Images

In the ever-evolving landscape of IT and DevOps, automation plays a pivotal role in streamlining workflows and ensuring efficiency. Ansible, a …

Learn Ansible – A Comprehensive Guide for Courses and Exams

Learn Ansible – A Comprehensive Guide for Courses and Exams

Introduction Ansible is a well-known open-source IT and application automation software which can be used to implement enterprise automation use …

Dynamic Scheduler in Ansible Automation Platform

Dynamic Scheduler in Ansible Automation Platform

This blog starts with one patching automation workshop. The customer’s procedure is that the end user raises a patching request. The request includes …

Decision Systems/Rule Base + Event-Driven Ansible

Decision Systems/Rule Base + Event-Driven Ansible

We use a rule base to find the new routing path with the lowest cost. RHDM gives the path from J to k. Kafka event is the communication channel for …

Introducing the Event-Driven Ansible & Demo

Introducing the Event-Driven Ansible & Demo

In AnsibleFest 2022, Red Hat announced an exciting new developer preview for Event-Driven Ansible. Event-Driven Ansible is a new way to enhance and …