Integration of Netbox inventory plugin in ansible runner

276 Views Asked by At

i try to run ansible playbooks directly from python code with a netbox dynamic inventory.

I have a netbox instance running locally and a netbox_inventory.yml file with the following content:

plugin: netbox.netbox.nb_inventory
api_endpoint: http://localhost:8000
token: 1bfa5d67dc4bf2f7d3ed967e956fba7831bab84c
validate_certs: False
config_context: False
group_by:
  - device_roles

device_query_filters:
  - has_primary_ip: 'true'

In my testcase i use the following code to run a playbook:

import ansible_runner

r = ansible_runner.run(private_data_dir=".", playbook='test.yml', inventory="netbox_inventory.yml", verbosity=5)

print(r.status)
print(r.rc)
print(r.events)
print(r.stats)

I always got no host back because he can not parse the inventory file. If i try to do an ansible-inventory -i netbox_inventory.yml --list it works like a charm.

The response of the python script is:

ansible-playbook [core 2.15.5]
  config file = None
  configured module search path = ['/Users/userx/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /Users/userx/_DEV/flask_celery_dev/venv/lib/python3.9/site-packages/ansible
  ansible collection location = /Users/userx/.ansible/collections:/usr/share/ansible/collections
  executable location = /Users/userx/_DEV/flask_celery_dev/venv/bin/ansible-playbook
  python version = 3.9.6 (default, Aug 11 2023, 19:44:49) [Clang 15.0.0 (clang-1500.0.40.1)] (/Users/userx/_DEV/flask_celery_dev/venv/bin/python)
  jinja version = 3.1.2
  libyaml = True
No config file found; using defaults
setting up inventory plugins
Loading collection ansible.builtin from 
host_list declined parsing /Users/userx/_DEV/flask_celery_dev/netbox_inventory.yml as it did not pass its verify_file() method
script declined parsing /Users/userx/_DEV/flask_celery_dev/netbox_inventory.yml as it did not pass its verify_file() method
Loading collection netbox.netbox from /Users/userx/_DEV/flask_celery_dev/venv/lib/python3.9/site-packages/ansible_collections/netbox/netbox
Using inventory plugin 'ansible_collections.netbox.netbox.plugins.inventory.nb_inventory' to process inventory source '/Users/userx/_DEV/flask_celery_dev/netbox_inventory.yml'
toml declined parsing /Users/userx/_DEV/flask_celery_dev/netbox_inventory.yml as it did not pass its verify_file() method
[WARNING]:  * Failed to parse
/Users/userx/_DEV/flask_celery_dev/netbox_inventory.yml with auto plugin: pytz
must be installed to use this plugin
  File "/Users/userx/_DEV/flask_celery_dev/venv/lib/python3.9/site-packages/ansible/inventory/manager.py", line 293, in parse_source
    plugin.parse(self._inventory, self._loader, source, cache=cache)
  File "/Users/userx/_DEV/flask_celery_dev/venv/lib/python3.9/site-packages/ansible/plugins/inventory/auto.py", line 59, in parse
    plugin.parse(inventory, loader, path, cache=cache)
  File "/Users/userx/_DEV/flask_celery_dev/venv/lib/python3.9/site-packages/ansible_collections/netbox/netbox/plugins/inventory/nb_inventory.py", line 2060, in parse
    self.main()
  File "/Users/userx/_DEV/flask_celery_dev/venv/lib/python3.9/site-packages/ansible_collections/netbox/netbox/plugins/inventory/nb_inventory.py", line 1901, in main
    raise_from(
  File "<string>", line 3, in raise_from
[WARNING]:  * Failed to parse
/Users/userx/_DEV/flask_celery_dev/netbox_inventory.yml with yaml plugin:
Plugin configuration YAML file, not YAML inventory
  File "/Users/userx/_DEV/flask_celery_dev/venv/lib/python3.9/site-packages/ansible/inventory/manager.py", line 293, in parse_source
    plugin.parse(self._inventory, self._loader, source, cache=cache)
  File "/Users/userx/_DEV/flask_celery_dev/venv/lib/python3.9/site-packages/ansible/plugins/inventory/yaml.py", line 114, in parse
    raise AnsibleParserError('Plugin configuration YAML file, not YAML inventory')
[WARNING]:  * Failed to parse
/Users/userx/_DEV/flask_celery_dev/netbox_inventory.yml with ini plugin:
Invalid host pattern 'plugin:' supplied, ending in ':' is not allowed, this
character is reserved to provide a port.
  File "/Users/userx/_DEV/flask_celery_dev/venv/lib/python3.9/site-packages/ansible/inventory/manager.py", line 293, in parse_source
    plugin.parse(self._inventory, self._loader, source, cache=cache)
  File "/Users/userx/_DEV/flask_celery_dev/venv/lib/python3.9/site-packages/ansible/plugins/inventory/ini.py", line 137, in parse
    raise AnsibleParserError(e)
[WARNING]: Unable to parse
/Users/userx/_DEV/flask_celery_dev/netbox_inventory.yml as an inventory source
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that
the implicit localhost does not match 'all'
Loading callback plugin default of type stdout, v2.0 from /Users/userx/_DEV/flask_celery_dev/venv/lib/python3.9/site-packages/ansible/plugins/callback/default.py
Loading callback plugin awx_display of type stdout, v2.0 from /Users/userx/_DEV/flask_celery_dev/venv/lib/python3.9/site-packages/ansible_runner/display_callback/callback/awx_display.py
Attempting to use 'awx_display' callback.
Skipping callback 'awx_display', as we already have a stdout callback.
Attempting to use 'default' callback.
Skipping callback 'default', as we already have a stdout callback.
Attempting to use 'junit' callback.
Attempting to use 'minimal' callback.
Skipping callback 'minimal', as we already have a stdout callback.
Attempting to use 'oneline' callback.
Skipping callback 'oneline', as we already have a stdout callback.
Attempting to use 'tree' callback.

PLAYBOOK: test.yml *************************************************************
Positional arguments: test.yml
verbosity: 5
connection: smart
timeout: 10
become_method: sudo
tags: ('all',)
inventory: ('/Users/userx/_DEV/flask_celery_dev/netbox_inventory.yml',)
forks: 5
1 plays in test.yml

PLAY [TEST] ********************************************************************
skipping: no hosts matched

PLAY RECAP *********************************************************************
successful
0
<generator object Runner.events at 0x103014f20>
{'skipped': {}, 'ok': {}, 'dark': {}, 'failures': {}, 'ignored': {}, 'rescued': {}, 'processed': {}, 'changed': {}}

I expect that i can also use a dynamic inventory with the ansible runner. I already verified that the dynamic inventory delivers data with ansible-inventory -i netbox_inventory.yml --list

0

There are 0 best solutions below