I’m attempting to make some things happen with nested playbooks. Seems like including full on playbooks (as opposed to “taskbooks”) from within “tasks” is not supported as described here. Doing vars, vars_files, vars_prompts are not supported when including at the playbook level is not supported. Also, doing variable substitution at this level is also tricky. Doing variable substitution with variables supplied at the inventory level (IE group_vars) is not supported. This also seems to be the case with group_vars at the playbook level. However, extra-vars are able to be substituted as described here.
I figured maybe I’d be able to get around some of these things by using callbacks or vars_plugins. Callbacks seem to happen after playbook parsing so that is a no go. The vars_plugins don’t seem to be able to access variables at the same scope that extra-vars are at. I can manipulate the ‘all’ group but that doesn’t seem to matter. Judging by the post in the previous link a dynamic inventory wont’ help.
Only solution I’m seeing currently is creating some sort of wrapper around ansible-playbook that passes the correct extra-vars or requiring my users to pass something which I’m not really fond of. Is there any way I can extend Ansible to allow for manipulating the extra-vars variables so I can do variable substitution at the playbook level?
Any help would be much appreciated. I am usually able to navigate around any Ansible problems I have but seem to be stuck on this one.
I’m in same desired state. Mine is simple, created a VLAN playbook and ran across two Arista switches & works fine.
But one of the guy asked me can we able to pick and choose VLAN ID, VLAN NAME like prompting users to enter the value and Ansible goes and configures to switches. Instead me going and writing VLAN ID and NAME on the playbook.
Please any advise would be greatly appreciated, I’m very new to this ansible world.
You CAN use vars_prompt in included plays, but plays cannot be included inside other plays. ‘include’ is a bit confusing as it is really 2 diff things ‘include_plays’ and ‘include_tasks’.
‘include_plays’ can ONLY occur at the top level, not inside other plays, ‘include_tasks’ can ONLY occur inside a play (roles are already inside a play).
So I’m trying to include this play at the top level while still doing variable substitution:
include: “{{ foo_version }}/site.yml”
vars_files:
“{{ inventory_dir }}/foo.yml”
When I supply “vars_files” at that top level “playbook” include it does not work. The variable substitution for “foo_version” does not work unless I run Ansible with extra vars like the following:
What about my other question in regards to manipulating Ansible’s extra vars in some sort of extension like vars_plugins for example? Seems like those can only be set on the command line. Any thoughts on that?
No, they can only be set on command line, that is what extra vars are.
Sadly, vars_plugins are really ‘inventory vars plugins’ and don’t really have access to many things nor work in a generic way. They might even go away once we revamp inventory (which was delayed in 2.0 and really needs some work).
Understood. In reality I don’t need “extra vars” in the strict sense. They just so happen to be the only variables that I have found that operate at the global scope that I need them at in order to do these playbook includes. If there is any mechanism that can operate on variables at this scope outside of extra vars I’d love to know.
Unfortunately, since playbook level includes can’t do vars, vars_prompts or vars_files and I can have the happen at a lower layer and playbooks can’t be included as tasks this seems like the only avenue to solve my dumb problem.