XO Ansible Dynamic Inventory Plugin - Group Variables Support?
-
Hello!
I'm working with the
community.general.xen_orchestradynamic 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: anotheradminI'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.ymlAnd then:
# gp1.yml ansible_user: admin# gp2.yml ansible_user: anotheradmin -
Many thanks @nathanael-h & @shinuza , both approaches indeed work!
-
O olivierlambert marked this topic as a question on
-
O olivierlambert has marked this topic as solved on
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login