Limiting hosts on included playbooks

Hi all,

I’m attempting to use a technique for limiting hosts in playbook includes described in an older thread ( https://groups.google.com/forum/#!msg/ansible-project/7Yb4zmZPg8c/b646JLYpJccJ ), and it’s mostly working, except for one thing. I have a playbook that includes another like this:

  • include: other_playbook.yml limit_hosts=a_group

In that included playbook, my hosts lines are all like this:

  • hosts: target_group:&{{ limit_hosts }}

which works great for limiting the included playbook tasks to the passed in group. However, I also need to be able to run that included playbook separately, at which point I need to set limit_hosts to ‘all’ for normal operation. I can do this on the command line with -e, or in a vars section, but the best place to put it (which would make things ‘just work’) seems to me to be my group_vars/all file, but that’s the one place that doesn’t work. Should it? Is there a better way to do this? I’m running a fairly recent ansible dev version ( 078aaa9 ), and have also tried with the latest as of this post ( f31cb7c ).

Nathan

have you tried?:

  • hosts: target_group:&{{ limit_hosts|default(‘all’)}}

I have, and it works, but then I have to set that for every play, and can’t set it for all of them at once.

Nathan

not sure what you mean by that.

in any case group_vars/all won't get picked until AFTER hosts: is
evaluated so you cannot use a variable from the inventory to define the
inventory you are going to use (self referential!).

By that I mean that I would much prefer to set this variable in one place for all plays, instead of having to define a default everywhere I need to use that variable.

Ah, I guess that makes sense about group_vars. Is there anywhere else I can set this as a default value?

Nathan