Excerpts from Michael DeHaan's message of 2013-07-09 21:09:21 -0400:
While this sounds great (and was technically pretty slick), tasks must be
uniform for all hosts in the play (but variables get subbed in later)
95% of everyone seems to want to use this with an inventory scoped data,
like having a list of packages or users defined in the group.
This doesn't work, because the same tasks can't be created for each item.
Just to clarify, this means looping on task includes *would* work if the
list were set in, say, 'group_vars/all'? And might *happen to* work if
the list were defined in, say, 'group_vars/foo', and the playbook in
question had 'hosts' set to 'foo'?
In other words, ansible parses the yaml, encounters an include statement
(either literally or via role magic), and then evaluates the loop. If
any variables referenced in the loop cannot be resolved, they are (like
all unresolved variables in ansible), passed literally.
I can see that coming back to bite you if you forget those specifics.
I assume the exact same conditions imply to looping roles?
Is there any chance of inserting a deprecation warning (or just a 'there
be dragons' warning) when someone uses a loop with an include statement
or a role statement?
If not, perhaps we could add a note in the documentation regarding
variable precedence? Or add a short section near there about scoping
rules (of which there are very few, this being by far the most
complicated)? I'd be happy to submit a pull request if that's desired.
I can also see wanting to let sleeping dogs lie...
Also, re: lookup plugins other than 'with_items', as Serge asked, I'm
going to guess is again matters whether the lookup plugin can be
evaluated.
So, for example, I tested the following:
--- # test.yaml
hosts: localhost
connection: local
tasks:
- include: mytasks.yaml
with_env:
- HOME
- USER
- PWD
- EDITOR
--- # mytasks.yaml
- debug: msg={{item}}
Running `ansible-playbook test.yaml` gives:
PLAY [localhost] **********************************************************
GATHERING FACTS ***********************************************************
ok: [localhost]
TASK: [debug msg=/home/chamill] *******************************************
ok: [localhost] => (item=/home/chamill)
TASK: [debug msg=chamill] *************************************************
ok: [localhost] => (item=chamill)
TASK: [debug msg=/home/chamill] *******************************************
ok: [localhost] => (item=/home/chamill)
TASK: [debug msg=vim] * ***************************************************
ok: [localhost] => (item=vim)
PLAY RECAP ****************************************************************
localhost : ok=4 changed=0 unreachable=0 failed=0
Anwyay, sorry about the wall of text, but I got curious. Basically
with_items works fine on includes, but only if the variables in the loop
have an effectively global scope (so, variables set for the 'all' group,
or directly in the playbook, or resolved at runtime via a lookup plugin;
I might be missing one or two other types of variables).