Extra-vars flag with file not populating variables with ansible-inventory command

I’m trying to integrate my netbox installation with ansible to use it as an inventory source of truth.

I’m merely trying to do the first type of integration which is to list the hosts in netbox.

My command

% ansible-inventory --list -i netbox_inv.yaml --extra-vars "@netbox_vars.yaml" -vvvv

netbox_inv.yaml:

---
plugin: netbox.netbox.nb_inventory
validate_certs: true
api_endpoint: "{{ api }}"
token: "{{ token }}"
config_context: false
group_by:
  - device_roles 

netbox_vars.yaml:

---
api: "https://mynetbox.local.lan"
token: "longlonglongtoken"

But when I run my command, the output looks like this:

ansible-inventory [core 2.17.6]
  config file = None
  configured module search path = ['/home/ansible/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/lib/python3.11/site-packages/ansible
  ansible collection location = /home/ansible/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/local/bin/ansible-inventory
  python version = 3.11.1 (main, Dec 21 2022, 08:24:23) [GCC 10.2.1 20210110] (/usr/local/bin/python)
  jinja version = 3.1.4
  libyaml = True
No config file found; using defaults
setting up inventory plugins
Loading collection ansible.builtin from 
host_list declined parsing /home/ansible/netbox_inv.yaml as it did not pass its verify_file() method
script declined parsing /home/ansible/netbox_inv.yaml as it did not pass its verify_file() method
Loading collection netbox.netbox from /usr/local/lib/python3.11/site-packages/ansible_collections/netbox/netbox
Using inventory plugin 'ansible_collections.netbox.netbox.plugins.inventory.nb_inventory' to process inventory source '/home/ansible/netbox_inv.yaml'
Fetching: {{ api }}/api/status
toml declined parsing /home/ansible/netbox_inv.yaml as it did not pass its verify_file() method

Note the “{{api}}” variable that is NOT substituted when trying to fetch from the API URL. When I put the actual URL and token values into the netbox_inv.yaml, things work.

Am I using the extra-vars flag incorrectly, or perhaps written the yaml wrong? Any help greatly appreciated

While this may change in the future, right now variables do not automatically work in inventory configs and plugin authors have to manually template things if they need to allow users to use variables. Looking at this plugin, it appears that the only thing that it templates is the token, so you cannot use a Jinja expression in api_endpoint.

1 Like

Wow, youre right. I can make token a variable, but not api_endpoint. While unfortunate, it’s a bit more secure than having neither of them. Thanks!