Ansible unexpected eval of variables when using flattened

178 Views Asked by At

Have have a configuration file looking like that:

one:
  some: 'conf'

foo:
  -
    bar:
      - 'one'
      - 'two'
      - 'three'
  -
    bar:
      - 'one'
      - 'four'
      - 'five'

I want to get a list containing all strings of bar lists. I did this task:

- name: My amazing task
  debug: var=item
  with_flattened:
    - "{{ foo | map(attribute='bar') | list }}"
    #- Another lists here, but removed for simplicity 

Here is the problem; the resulting list looks like this:

[{"one": "some": "conf"}, "two", "three", {"one": "some": "conf"}, "four", "five"]

Ansible seems to interpret the “one” variable previously set, ignoring the fact I’m expecting a string.

What did I do wrong? How can I get a list of strings out of the bar variables configuration?

(I use Ansible 1.9)

1

There are 1 best solutions below

0
Konstantin Suvorov On BEST ANSWER

To prevent this very issue bare variables in with_ loops don't work in recent Ansible releases.

To handle it in your case, use:

with_items: "{{ foo | map(attribute='bar') | sum(start=[]) | list }}"

sum(start=[]) makes a list of lists flat here.