ansible 2.5 with_items -> loop conversion

So I’ve been looking at the porting guide for 2.5 (i’m going from 2.4 to 2.7).

I have this task that outputs a list of version numbers as strings and registers the output. I then loop through the registered variable using with_items. I’ve been looking at converting this to loop. I realize that with_items isn’t deprecated (yet), but i’m just trying to be proactive.

I’m running into issues with how loop iterates over register.stdout_lines var vs with_items.

In addition, I noticed that if i register lines that start with numbers, instead of words, the result of with_items is different, throwing variable is not defined errors.
Here’s a simplified version of what i’m doing with two different sets of variables to illustrate the differences.
https://pastebin.com/ai1Bu9G0

So my questions are:

  • How do I convert with_items to loop when used in conjunction with a registered var register.stdout_lines?
  • And as a secondary question, why does with_items treat numbers vs characters differently?

When you switch from this syntax using with_items:

with_items:

  • “{{ numberedvars.stdout_lines }}”

Your loop should not look the same, instead it should be:

loop: “{{ numberedvars.stdout_lines }}”

with_items does something called flattening, which will take a nested list of lists, and flatten it. loop does not do flattening, and by using the - on a new line, indicates that you are creating a list element in YAML.

I cannot necessarily answer your numbers vs characters question, without more context.

Thanks Matt! That was indeed the issue!

The other question isn’t important, so don’t waste time on it unless you’re just curious. But if you look at the output between the two variable examples, numberedvars vs wordvars towards the bottom, they are different in what it outputs (one gives “proper” output while the other gives VARIABLE IS NOT DEFINED).

Again, not super important, as i’m not trying to solve that issue, just something I noticed.