XO Ansible Dynamic Inventory Plugin - Group Variables Support?
-
Hello!
I'm working with the
community.general.xen_orchestra
dynamic inventory plugin and trying to implement group-specific variables through the tagging function.For instance, here's an example of a static inventory I currently have. Nothing fancy.
gp1: vars: ansible_user: admin hosts: server1.example.com: server2.example.com: gp2: vars: ansible_user: anotheradmin hosts: server3.example.com: server4.example.com:
The following dynamic inventory configuration works fine:
plugin: community.general.xen_orchestra api_host: xo.example.com user: admin password: mypass validate_certs: true use_ssl: true groups: gp1: "'gp1' in tags" gp2: "'gp2' in tags"
But adding group vars breaks it:
[...] groups: gp1: "'gp1' in tags" vars: ansible_user: admin gp2: "'gp2' in tags" vars: ansible_user: anotheradmin
I'm wondering if the plugin supports adding group-specific variables directly in the dynamic inventory configuration.
Would appreciate hearing if anyone has made this work and the most elegant and supported solution. I looked extensively through the forum, blog posts, etc. and could not figure it out.
Thanks in advance for your help!
-
Adding @shinuza and @nathanael-h in the loop
-
Hello @guiand888
You could use compose in your inventory file like this:
plugin: community.general.xen_orchestra api_host: xo.example.com user: admin password: mypass validate_certs: true use_ssl: true groups: gp1: "'gp1' in tags" gp2: "'gp2' in tags" compose: ansible_user: "'admin' if 'gp1' in tags else 'anotheradmin' if 'gp2' in tags"
I made some local tests, this should do what you need. Maybe @shinuza has a better suggestion for writing the Jinja2 expression in the compose key.
-
@guiand888 Hey, the plugin does support composite vars. However I believe the idiomatic way to do what you want is this:
group_vars/ gp1.yml gp2.yml
And then:
# gp1.yml ansible_user: admin
# gp2.yml ansible_user: anotheradmin