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