Append to list during playbook executions for all hosts

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!

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.

Hi,

I see you already use facts cache:

fact_caching = jsonfile
fact_caching_connection = cache

Note that fact_caching_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.

Here is an example playbook for a similar usecase; perhaps you’ll spot something missing, but your config is looking fine to me.

2 Likes