Skip to content Skip to sidebar Skip to footer

Ansible Jinja2 Merging Lists To A Single List

I am trying to iterate a list ['abc','def','ghi'] & each iteration generates a list which i need to set it to a variable in ansible. here is my current script: - name: add chec

Solution 1:

which generates the following output which is a string & not a list

It's a string for two reasons: first off, you embedded a " + " bit of text in the middle of your expression, and the second is because you called join(',') and jinja cheerfully did as you asked.

how can i append to the single list similar to list += temp_list in a for loop

The answer is to do exactly as you said and use an intermediate variable:

CHECKS: >-
  {%- set tmp = CHECKS | default([]) -%}
  {%- for cKey in checkKey -%}
  {%-   set _ = tmp.extend(CHECKSMAP | map(attribute=cKey ) | list) -%}
  {%- endfor -%}
  {{ tmp }}

AFAIK, you have to use that .extend trick because a set tmp = tmp + will declare a new tmp inside the loop, rather than assigning the tmp outside the loop

Post a Comment for "Ansible Jinja2 Merging Lists To A Single List"