Hi All
there is very little information, but correct me if i’m wrong the doc states that ansible-inventory has the extra vars option
From ansible-inventory doc:
(can’t paste the link as im a new user, can only paste 2 links )
-e, --extra-vars
set additional variables as key=value or YAML/JSON, if filename prepend with @. This argument may be specified multiple times.
From ansible-playbook doc:
-e, --extra-vars
set additional variables as key=value or YAML/JSON, if filename prepend with @. This argument may be specified multiple times.
There’s literally nothing in those two pages that would give you the impression that it wasn’t the same option and did the same thing…
So, you’d assume straight off the bat, that it is the same functionality as running ansible-playbook with the extra vars option (-e, --extra-vars)
So anyway, the thing is, I try to use it as so:
ansible-inventory -i inventory/dynamic/tt-per-customer-mysql.yml --graph -e customer="My Fav Customer"
The var isn’t being interpolated within the inventory file:
# MySQL Dynamic Inventory to query hosts based on customer
#
# Required columns:
# 'inventory_group', 'inventory_hostname'
#
# TLDR:
# Besides the required columns, each column will be assigned
# as an ansible hostvar dynamically.
plugin: mysql-dynamic-inventory
db_host: mysql_host
db_user: ansible
db_pass: mysql_pass
db_name: icingadb
db_query: |
SELECT
host as inventory_hostname,
address as ansbile_host,
'openwrt' as inventory_group
WHERE customer = "{{customer}}"
Using a custom mysql dynamic inventory plugin that i made, that works fine… just not with the extra var
Troubleshooting:
I have created tried creating a new plugin option
test:
description: test
required: true
And in the plugin python code i did:
print(self.get_option('test'))
then in the inventory file i just assigned the option like so:
plugin: mysql-dynamic-inventory
db_host: mysql_host
db_user: ansible
db_pass: mysql_pass
db_name: icingadb
test: "{{customer}}"
The output after running ansible-inventory with the -e customer=“My Fav Customer” was:
{{customer}}
So it isn’t interpolating the correctly, not sure how / what else to try - i heard a few times @bcoca mention that this is an option of the plugin:
https://github.com/ansible/ansible/issues/77339#issuecomment-1077656939
So it seems like its doable, but how on earth… i can’t find any doc, i’ve tried as best i can to reverse engineer the inventory plugin: - ansible.builtin.constructed ( inspecting the python plugin code ansible/blob/devel/lib/ansible/plugins/inventory/constructed.py
)
Which is claimed to support extra vars, but i can’t figure out if it’s missing completely or i’m just not able to figure it out… lack of skillz
Can anyone shine some light on this ?
Would be forever grateful
Thanks in advance, sorry for the long essay of a question just wanted to articulate it as best I could,