# Appending to fact provided by module, and use aggregated fact in module function

**URL:** <https://forum.ansible.com/t/appending-to-fact-provided-by-module-and-use-aggregated-fact-in-module-function/20605>\
**Category:** Ansible Project\
**Created:** [February 17, 2016, 5:49pm UTC](https://forum.ansible.com/t/appending-to-fact-provided-by-module-and-use-aggregated-fact-in-module-function/20605 "2016-02-17T17:49:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tim\_Saeteroy](https://avatars.discourse-cdn.com/v4/letter/t/90db22/32.png) [@Tim\_Saeteroy](https://forum.ansible.com/u/Tim_Saeteroy)\
**Post date:** [February 17, 2016, 5:49pm UTC](https://forum.ansible.com/t/appending-to-fact-provided-by-module-and-use-aggregated-fact-in-module-function/20605/1 "2016-02-17T17:49:39Z")

</div>

Hi,

- How would you append to a dict or list fact using `ansible_facts` spanning multiple module calls?
- How can the result be used internally in another function of the module?

Let’s say we have a module that is designed to be called multiple times, like so, each time creating a new file with some content:

``  
`

- name: Add Foo  
mymodule:  
name: foo  
id: 1  
content: “lorem ipsum”

- name: Add Bar  
mymodule:  
name: bar  
id: 2  
content: “dolor sit amet”  
`

Each call to the module want to return a part of a dict using `ansible_facts`:

`  
facts = {mymodule: {id: name}}  
module.exit\_json(changed=True, ansible\_facts=facts}

`

After these two calls, the fact should contain:

`  
“mymodule”: {  
1: “foo”  
2: “bar”  
}

`

Following the additions, there’s a final call to the module to trigger a function that should write the fact to a index file.

(A more real world example could be that files in a directory not listed in the fact, should be deleted)

`

- name: Save mymodule additions  
mymodule:  
save: /home/user/mymodule.txt

`

How should the facts be returned in order for them to be appended, and not overwritten?

I’d rather not have use unique facts for each entry, e.g. `"mymodule_1" = "Foo"`. Instead, I want to access the fact as a dict or list to be used internally in the module.

I realise I may be overthinking this, but I’m new to Ansible module development and I wasn’t able find any examples on how module provided facts should be used in a scenario like this.

Thanks!

- Tim

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [February 25, 2016, 3:46am UTC](https://forum.ansible.com/t/appending-to-fact-provided-by-module-and-use-aggregated-fact-in-module-function/20605/2 "2016-02-25T03:46:21Z")

</div>

Sorry, currently there is no way to append facts, they always overwrite.

---

<div class="post-metadata">

**Author:** ![Tim\_Saeteroy](https://avatars.discourse-cdn.com/v4/letter/t/90db22/32.png) [@Tim\_Saeteroy](https://forum.ansible.com/u/Tim_Saeteroy)\
**Post date:** [February 26, 2016, 9:43pm UTC](https://forum.ansible.com/t/appending-to-fact-provided-by-module-and-use-aggregated-fact-in-module-function/20605/3 "2016-02-26T21:43:44Z")

</div>

All right. Thanks for the clarification.

- Tim
