[Solved] Create set_fact list with loop not working

Patrick
Mon, 24 Nov 2025 18:45:32 +0000
Get Help

Hi, I am trying to create with set_fact a list of facts with loop but I can’t get it to work. Can anyone please give me a hint what I’m doing wrong? I want a simple list so I can use it with ‘when’.

The zones.results is the output from:

- name: check if zones are present
  shell: sudo -u pdns pdnsutil show-zone {{ item }}
  with_items: "{{ pdns_zones }}"
  register: zones

which generates a gigantic dictionary(?) with multiple levels.

- name: set empty myfact
  set_fact:
    myfact: []

- name: set myfact
  set_fact:
    myfact: "{{ myfact + ['{{ item.item}} {{ item.stdout }} {{ item.stderr }}'] }}"
 loop: "{{ zones.results }}"

- name: debug facts
  debug:
    msg: "{{ status }}"

TASK [debug facts] ************************************************************************************************
ok: [example.org] => {
    "msg": [
        "domain1.org This is a Master zone...snip",
        "{{ item.item}} {{ item.stdout }} {{ item.stderr }}",
        "{{ item.item}} {{ item.stdout }} {{ item.stderr }}",
        "{{ item.item}} {{ item.stdout }} {{ item.stderr }}"
    ]
}

There are 4 domains in zones.results so the number of elements are correct but the item.item etc obviously are not. How to make this work? Thanks!

Solved: the solution is to use:

- name: set status fact
  set_fact:
      status: "{{ status + [ item.item + ' ' + item.stdout + ' ' + item.stderr ]  }}"
  loop: "{{ zones.results }}"

2 posts - 2 participants