I apologize for asking this because I see a number of previous threads, but none of them hit exactly this point, or the answers don’t appear to have worked for me yet so I’m trying to figure out if I’m just completely missing something.
I have state that I need to persist between invocations of my module.
I’m following http://docs.ansible.com/developing_modules.html#module-provided-facts, but it appears to be pretty scarce (I know documentation is hard, so I’d love to contribute back the answer to the question I’m about to ask, since it’d probably fit there quite reasonably).
I have a list of strs which my module should read, possibly modify, and then store back.
The pattern for this, from various sequences of grepping around the example modules (which don’t seem to do this), reading the PlayBook and Runner source, and just jumping back and forth, seems like it should be something like:
…
facts = ansible_facts(module)
existing_services = facts.get(“services”, {})
new_services = do_stuff_with(services)
module.exit_json(…, ansible_facts={“services” : new_services}
But after the module runs once, nothing new appears in the facts (specifically, there’s no new “services” key, and no other new keys, and even assert "somethingIknowshouldbethere" in str(facts)
fails so it doesn’t appear to get added to some subobject in there.).
Where should my keys be going?
https://github.com/ansible/ansible/blob/devel/lib/ansible/playbook/__init__.py#L519 makes it seem like the answer is “top-level”, but yeah, I don’t see them, so I must be missing something.
If my example isn’t concrete enough and in fact it looks correct, here’s my exact module: (preface: my slightly embarrassing internal comment is now public due to me pushing that to get it working – apologies, it’s been a slightly long day of frustration): https://github.com/Magnetic/nix-ansible/blob/3d37bceefe7b022ee0cd9ad31e6c3b773e6b4fe4/nix.py
As a tangent, maybe this function (get_all_facts) isn’t supposed to be public, I don’t see any documentation for it, and specifically about what kind of object it wants as an argument, but https://github.com/ansible/ansible/blob/devel/lib/ansible/module_utils/facts.py#L2716-L2717 seems slightly strange, it seems to be expecting a module with a “filter” param specifically. Does that “# ======” section break mean “from here on down is private” or something, or why is it making that assumption? (I see that that code appears to be copy-pasted from the setup
module, so maybe the answer is “it shouldn’t, and it was just copied over from there, which does have a filter parameter?”)
Appreciated,
-Julian