Skip to content

Inventory plugin loader ignores NAME attribute and uses filename instead #2431

Open
@Yannik

Description

@Yannik

Summary

The NAME configured for an inventory plugin is ignored by the plugin loader (not set as plugin name, nor as alias), and cannot be used with ANSIBLE_INVENTORY_ENABLED.

Issue Type

Bug Report

Component Name

plugins/loader.py

Ansible Version

$ ansible --version
ansible [core 2.18.2]
  config file = None
  configured module search path = ['/home/yannik/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /home/yannik/venv/lib/python3.12/site-packages/ansible
  ansible collection location = /home/yannik/.ansible/collections:/usr/share/ansible/collections
  executable location = /home/yannik/venv/bin/ansible
  python version = 3.12.3 (main, Jan 17 2025, 18:03:48) [GCC 13.3.0] (/home/yannik/venv/bin/python3)
  jinja version = 3.1.5
  libyaml = True

Configuration

$ ansible-config dump --only-changed -t all
CONFIG_FILE() = None
GALAXY_SERVERS:

OS / Environment

Ubuntu 24.04

Steps to Reproduce

Create inventory_plugins/dynamic-inventory-plugin.yml

from ansible.plugins.inventory import BaseInventoryPlugin

class InventoryModule(BaseInventoryPlugin):
    NAME = ' myorg.mygroup.custom_inventory '

    def verify_file(self, path):
        return True

    def parse(self, inventory, loader, path, cache=True):
        # call base method to ensure properties are available for use with other helper methods
        super(InventoryModule, self).parse(inventory, loader, path, cache)

        self.inventory.add_host('localhost')

Then execute ANSIBLE_INVENTORY_ENABLED=myorg.mygroup.custom_inventory ANSIBLE_INVENTORY_PLUGINS=$(pwd)/inventory_plugins ansible-inventory –list

Expected Results

The command should list the host localhost defined in the inventory plugin.

Actual Results

The following error occurs:

[WARNING]: Failed to load inventory plugin, skipping myorg.mygroup.custom_inventory
ERROR! No inventory plugins available to generate inventory, make sure you have at least one enabled.

By appending a couple of lines of debugging code to plugins/loader.py:

for plugin in inventory_loader.all():
  display.warning("_original_path: %s" % plugin._original_path)
  display.warning("_load_name: %s " % plugin._load_name)
  display.warning("ansible_name %s " % plugin.ansible_name)
  display.warning("aliases: %s " % plugin.ansible_aliases)

we can observe that NAME is ignored and instead the filename is set as plugin name and alias:

[WARNING]: _original_path: /home/yannik/ansible-test/inventory_plugins/dynamic-inventory-plugin.py
[WARNING]: _load_name: dynamic-inventory-plugin
[WARNING]: ansible_name dynamic-inventory-plugin
[WARNING]: aliases: ['dynamic-inventory-plugin']

Code of Conduct

  • I agree to follow the Ansible Code of Conduct

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingnew_contributorThis PR is the first contribution by a new community member.

    Type

    No type

    Projects

    Status

    🆕 Triage

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions