# \[Solved\] Create set\_fact list with loop not working

**URL:** <https://forum.ansible.com/t/solved-create-set-fact-list-with-loop-not-working/44892>\
**Category:** Get Help\
**Tags:** loop, facts, ansible-core\
**Created:** [November 24, 2025, 6:45pm UTC](https://forum.ansible.com/t/solved-create-set-fact-list-with-loop-not-working/44892 "2025-11-24T18:45:32Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Patrick](https://avatars.discourse-cdn.com/v4/letter/p/b2d939/32.png) [@Patrick](https://forum.ansible.com/u/Patrick)\
**Post date:** [November 24, 2025, 6:45pm UTC](https://forum.ansible.com/t/solved-create-set-fact-list-with-loop-not-working/44892/1 "2025-11-24T18:45:32Z")

</div>

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:

```auto
- 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.

```auto
- 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:

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

```

---

<div class="post-metadata">

**Author:** ![bcoca](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bcoca/32/9269_2.png) [@bcoca](https://forum.ansible.com/u/bcoca)\
**Post date:** [November 24, 2025, 9:38pm UTC](https://forum.ansible.com/t/solved-create-set-fact-list-with-loop-not-working/44892/2 "2025-11-24T21:38:11Z")

</div>

also avoids ‘pre set\_fact’ task:

```auto
- name: set status fact
  set_fact:
      status: "{{ status|default([]).append( ' '.join([item.item, item.stdout, item.stderr])) }}"
  loop: "{{ zones.results }}"

```
