# Append to list during playbook executions for all hosts

**URL:** <https://forum.ansible.com/t/append-to-list-during-playbook-executions-for-all-hosts/3440>\
**Category:** Get Help\
**Created:** [January 23, 2024, 9:44am UTC](https://forum.ansible.com/t/append-to-list-during-playbook-executions-for-all-hosts/3440 "2024-01-23T09:44:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![chtsalid](https://avatars.discourse-cdn.com/v4/letter/c/ed655f/32.png) [@chtsalid](https://forum.ansible.com/u/chtsalid)\
**Post date:** [January 23, 2024, 9:44am UTC](https://forum.ansible.com/t/append-to-list-during-playbook-executions-for-all-hosts/3440/1 "2024-01-23T09:44:19Z")

</div>

Hi,

I would like to append to a list of dictionaries between the playbook executions for all hosts. However it seems that the list does not retain the appended items.

I am using ansible [core 2.13.13].

Here is my playbook:

* * *

- name: “Test”  
hosts: all\_router  
gather\_facts: no  
vars:  
backup\_status:   
tasks:
  - name: add items to list  
set\_fact:  
backup\_status: “{{ backup\_status + [{‘hostname’: inventory\_hostname, ‘status’: ‘Succeeded’}] }}”  
cacheable: yes  
delegate\_to: localhost
  - name: debug  
debug:  
msg: “{{ backup\_status }}”

Output:

TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [172.x1.y1.z1] =\> {  
“msg”: [  
{  
“hostname”: “172.x1.y1.z1”,  
“status”: “Succeeded”  
}  
]  
}  
ok: [172.x2.y2.z2] =\> {  
“msg”: [  
{  
“hostname”: “172.x2.y2.z2”,  
“status”: “Succeeded”  
}  
]  
}  
ok: [172.x3.y3.z3] =\> {  
“msg”: [  
{  
“hostname”: “172.x3.y3.z3”,  
“status”: “Succeeded”  
}  
]  
}

Here is my ansible.cfg file

[defaults]  
inventory = inventory.ini  
host\_key\_checking = False  
interpreter\_python=auto\_silent  
display\_skipped\_hosts = false  
fact\_caching = jsonfile  
fact\_caching\_connection = cache

It looks like that it doesn’t retain the entries between different executions.

Thanks in advance for your help!

---

<div class="post-metadata">

**Author:** ![chris](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/chris/32/293_2.png) [@chris](https://forum.ansible.com/u/chris)\
**Post date:** [January 23, 2024, 10:47am UTC](https://forum.ansible.com/t/append-to-list-during-playbook-executions-for-all-hosts/3440/2 "2024-01-23T10:47:27Z")

</div>

I think you would need to do something like write the result to a file at the end of each run and then load it at the start of the next.

---

<div class="post-metadata">

**Author:** ![ptn](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/ptn/32/481_2.png) [@ptn](https://forum.ansible.com/u/ptn)\
**Post date:** [January 23, 2024, 11:59am UTC](https://forum.ansible.com/t/append-to-list-during-playbook-executions-for-all-hosts/3440/3 "2024-01-23T11:59:06Z")

</div>

Hi,

I see you already use facts cache:

```ini
fact_caching = jsonfile
fact_caching_connection = cache

```

Note that [`fact_caching_connection`](https://docs.ansible.com/ansible/latest/reference_appendices/config.html#cache-plugin-connection) defines path of the json file you’re storing your facts in, so be sure this path is accessible to the user running you playbook.

Now, you’re showing output for a single run; subsequent ones should update cache and show you updated content in your debug task as well. Is that not the case ?  
If not, first ensure your are indeed using your ansible.cfg file, as [there might be precedence at play here](https://docs.ansible.com/ansible/latest/reference_appendices/general_precedence.html#configuration-settings).

[Here](http://forum.ansible.com/t/dynamic-value-assignment-to-ansible-fact-based-on-condition/706/9) is an example playbook for a similar usecase; perhaps you’ll spot something missing, but your config is looking fine to me.
