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

62 views
Skip to first unread message

Tim Sæterøy

unread,
Feb 17, 2016, 9:05:12 PM2/17/16
to Ansible Project
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

Brian Coca

unread,
Feb 24, 2016, 10:46:28 PM2/24/16
to ansible...@googlegroups.com
Sorry, currently there is no way to append facts, they always overwrite.


----------
Brian Coca

timss

unread,
Feb 26, 2016, 4:43:45 PM2/26/16
to Ansible Project
All right. Thanks for the clarification.

- Tim
Reply all
Reply to author
Forward
0 new messages