In version 1.2, we cannot iterate over a list of variables. Ansible now returns the fatal error “with_items expects a list.” I need to reference a group variable to get a list of items. We’ve used this in a few different situations and now we need a another way to do this or a fix for this issue.
> Correct, with_items needs a list, not a hash.
>
This used to work with hashes too (iterating over hash keys). Is there
any reason for not supporting this anymore?
You can now use Python snippets here, try doing this to turn the dict into
a list with the values:
when I went hunting for something similar. Not being a Python hacker, and thinking variables in this context where pure Jinja2 variables, I ended up using the dictsort filter, so the original example could be done like so
True, for this play. But if you had an additional play where you wanted to
look up the alias for a particular user, you can't use the same data.
For one play, the data is most simply expressed as a list of hashes, but
for the other, its a hash. Duplicating the data is obviously problematic.
There may be some Python trick to pick out an element of a list by a key,
but its beyond me. I believe we can't use list comprehensions.
Suite 1415
401 Docklands Drive
Docklands VIC 3008 Australia
"All parts should go together without forcing. You must remember that
the parts you are reassembling were disassembled by you. Therefore,
if you can't get them together again, there must be a reason. By all
means, do not use a hammer." -- IBM maintenance manual, 1925
So what seems to occur to me here is that “with_items” is clearly a list iteration, but it might make sense to make a “with_dictionary” that allows you to get {{ item.key }} and {{ item.value }} seperately.
And then if the value was a dictionary, it would be like {{ item.value.somekey }}
Nice thought, although I would prefer using "with_keys", "{{ key.name
}}" and "{{ key.value }}". Same concept, better naming, imho. And it's
consistent with "with_items".
I had this problem a few months back, and wrote a pair of custom lookup plugins to do the job. (See below.) Was meaning to contribute it back but so far hadn't gotten around to it yet.
The examples below haven't been tested recently, so no guarantees they still work. But assuming they do, you can just put them in a directory, let's call it $library, define $ANSIBLE_LIBRARY to include it, then you use them via "with_keys" and "with_values".
Also, I think that it would be more reasonable and closer to Ansible's
linear execution model to map YAML dictionaries to OrderedDict objects
(http://docs.python.org/2/library/collections.html#ordereddict-objects),
so that a 'with_keys' implementation iterates through the keys in the
order they are written in YAML code. The only problem is that
OrderedDict is available in Python version >= 2.7, but Ansible could
fallback to a backported version if available, or to an ordinary dict if
not.
I like with_keys; especially ordered! Little wary about falling back quietly to unordered. What about adding a companion attr? Say: unordered_ok=True ? Just looking to make the fallback explicit.