Skip to content

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

Avatar photo

https://www.linkedin.com/in/gineesh/ https://twitter.com/GiniGangadharan

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

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.
  • 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:

Disclaimer:

The views expressed and the content shared in all published articles on this website are solely those of the respective authors, and they do not necessarily reflect the views of the author’s employer or the techbeatly platform. We strive to ensure the accuracy and validity of the content published on our website. However, we cannot guarantee the absolute correctness or completeness of the information provided. It is the responsibility of the readers and users of this website to verify the accuracy and appropriateness of any information or opinions expressed within the articles. If you come across any content that you believe to be incorrect or invalid, please contact us immediately so that we can address the issue promptly.

Avatar photo


https://www.linkedin.com/in/gineesh/ https://twitter.com/GiniGangadharan
Gineesh Madapparambath is the founder of techbeatly and he is the author of the book - ๐—”๐—ป๐˜€๐—ถ๐—ฏ๐—น๐—ฒ ๐—ณ๐—ผ๐—ฟ ๐—ฅ๐—ฒ๐—ฎ๐—น-๐—Ÿ๐—ถ๐—ณ๐—ฒ ๐—”๐˜‚๐˜๐—ผ๐—บ๐—ฎ๐˜๐—ถ๐—ผ๐—ป. He has worked as a Systems Engineer, Automation Specialist, and content author. His primary focus is on Ansible Automation, Containerisation (OpenShift & Kubernetes), and Infrastructure as Code (Terraform). (aka Gini Gangadharan - iamgini.com)

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.