0
votes

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 checks
  set_fact:
    CHECKS: "{% for cKey in checkKey %} {{ CHECKS|default([]) }} + {{ CHECKSMAP | map(attribute=cKey ) | list |join(',')}} {% endfor %}"

which generates the following output which is a string & not a list how can i append to the single list similar to list += temp_list in a for loop

ok: [127.0.0.1] => {
"msg": "System  [] + [{u'check': u'system_checks'}, {u'check': u'lms_server_health'}]  [] + [{u'check': u'system_checks'}, {u'check': u'config-service_server_health'}, {u'check': u'config-service_server_restart'}] "   }
1
I coudn't find a question mark in your question. What's your question is?George Shuklin

1 Answers

2
votes

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