Ansible Inventory Plugin

111 views
Skip to first unread message

Kai Pistorius

unread,
Jan 12, 2024, 8:19:31 AM1/12/24
to Ansible Project
I am trying to develop a Ansible Inventory Plugin based on some examples I found on Github.
This is my Yaml File:
---
plugin: oracle_inventory
oem_db_server: myoem.domain.com
oem_db_port: 1521
oem_db_sid: OEM
oem_schema_owner: SYSMAN
oem_owner_password: password
oem_target_read_user: READ_ONLY_USER
oem_target_read_password: password
And this is my implementation of the Plugin:
from __future__ import (absolute_import, division, print_function)

from loguru import logger

__metaclass__ = type

DOCUMENTATION = r'''
    name: oracle_inventory
    plugin_type: inventory
    short_description: Returns Ansible inventory from Oracle databases
    description: Returns Ansible inventory from Oracle databases which are registered in Oracle Enterprise Manager (OEM)
    options:
        plugin:
            description: Name of the plugin
            required: true
            choices: ['oracle_inventory']
        oem_db_server:
            description: The OEM repository database server
            required: true
        oem_db_port:
            description: The Port of the OEM repository database
            required: true
        oem_db_sid:
            description: The ORACLE_SID of the OEM repository database
            required: true
        oem_schema_owner:
            description: The owner of the OEM repository schema
            required: true
        oem_owner_password:
            description: The password of the OEM repository schema owner
            required: true
        oem_target_read_user:
            description: The user which is allowed to query the host, agent, and database OEM targets and its properties
            required: true
        oem_target_read_password:
            description: The password of the OEM target read user
            required: true
'''

from ansible.plugins.inventory import BaseInventoryPlugin
from ansible.errors import AnsibleError, AnsibleParserError


class InventoryModule(BaseInventoryPlugin):
    NAME = 'oracle_inventory'

    def __init__(self):
        super(InventoryModule, self).__init__()

    def verify_file(self, path):
        '''Return true/false if this is possibly a valid file for this plugin to consume
        '''
        valid = False
        if super(InventoryModule, self).verify_file(path):
            logger.info(path)
            if path.endswith(('oracle_inventory.yaml',
                              'oracle_inventory.yml')):
                valid = True
        return valid
   
    def parse(self, inventory, loader, path, cache):
        '''Return dynamic inventory from source '''
        super(InventoryModule, self).parse(inventory, loader, path, cache)

        try:
            pass
            # self.oem_db_server = self.get_option("oem_db_server")
            # self.oem_db_sid = self.get_option("oem_db_port")
            # self.oem_db_port = self.get_option("oem_db_port")
            # self.oem_schema_owner = self.get_option("oem_schema_owner")
            # self.oem_schema_password = self.get_option("oem_owner_password")
            # self.oem_target_reader_user = self.get_option("oem_target_read_user")
            # self.oem_target_reader_password = self.get_option("oem_target_read_password")
        except Exception as e:
            raise AnsibleError(f"Options required: {e}")
        self._populate()
       
    def _populate(self):
        '''Return the hosts and groups'''
        self.inventory.add_group("MY_DEPARTMENT")
        self.inventory.add_host(host = "myserver.domain", group="MY_DEPARTMENT")
        self.inventory.set_variable("myserver.domain", 'ansible_host', "myserver.domain")


Both files are located in /home/kai/inventory_plugins

When I execute 
ansible-inventory -vvv -i oracle_inventory.yml --list

I get this warnings:
kai@myserver:/home/kai/inventory_plugins:ansible-inventory -vvv -i oracle_inventory.yml --list
ansible-inventory [core 2.16.2]
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/home/kai/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/lib/python3.11/site-packages/ansible
  ansible collection location = /home/kai/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/local/bin/ansible-inventory
  python version = 3.11.0 (main, Apr  4 2023, 13:20:21) [GCC 4.8.5] (/usr/local/bin/python3.11)
  jinja version = 3.1.2
  libyaml = True
Using /etc/ansible/ansible.cfg as config file
redirecting (type: inventory) ansible.builtin.virtualbox to community.general.virtualbox
host_list declined parsing /home/kai/inventory_plugins/oracle_inventory.yml as it did not pass its verify_file() method
ansible_collections.community.general.plugins.inventory.virtualbox declined parsing /home/kai/inventory_plugins/oracle_inventory.yml as it did not pass its verify_file() method
[WARNING]:  * Failed to parse /home/kai/inventory_plugins/oracle_inventory.yml with yaml plugin: Plugin configuration YAML file, not YAML inventory
  File "/usr/local/lib/python3.11/site-packages/ansible/inventory/manager.py", line 293, in parse_source
    plugin.parse(self._inventory, self._loader, source, cache=cache)
  File "/usr/local/lib/python3.11/site-packages/ansible/plugins/inventory/yaml.py", line 114, in parse
    raise AnsibleParserError('Plugin configuration YAML file, not YAML inventory')
[WARNING]:  * Failed to parse /home/kai/inventory_plugins/oracle_inventory.yml with constructed plugin: Incorrect plugin name in file: oracle_inventory
  File "/usr/local/lib/python3.11/site-packages/ansible/inventory/manager.py", line 293, in parse_source
    plugin.parse(self._inventory, self._loader, source, cache=cache)
  File "/usr/local/lib/python3.11/site-packages/ansible/plugins/inventory/constructed.py", line 142, in parse
    self._read_config_data(path)
  File "/usr/local/lib/python3.11/site-packages/ansible/plugins/inventory/__init__.py", line 235, in _read_config_data
    raise AnsibleParserError("Incorrect plugin name in file: %s" % config.get('plugin', 'none found'))
[WARNING]: Unable to parse /home/kai/inventory_plugins/oracle_inventory.yml as an inventory source
[WARNING]: No inventory was parsed, only implicit localhost is available
{
    "_meta": {
        "hostvars": {}
    },
    "all": {
        "children": [
            "ungrouped"
        ]
    }
}

Could anyone help me solving this?

Endre Szabo

unread,
Jan 12, 2024, 8:23:24 AM1/12/24
to ansible...@googlegroups.com
You don't need to have that Yaml file parsed/verified from within your plugin.
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/796159e7-e791-43da-b486-096213a2cb7cn%40googlegroups.com.

Kai Pistorius

unread,
Jan 12, 2024, 9:08:59 AM1/12/24
to Ansible Project
I found the error. I had to enable the auto and script plugin.

Brian Coca

unread,
Jan 12, 2024, 10:51:26 AM1/12/24
to ansible...@googlegroups.com
either the auto (which will try to find your custom plugin) or your
custom plugin need to be enabled.


--
----------
Brian Coca (he/him/yo)

Reply all
Reply to author
Forward
0 new messages