On 08/04/15 15:52 -0700, Julian Berman wrote:
>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)
^ This will retrieve the base ansible facts, so unless you need to consume
them, you wouldn't want to do this (also, you'd need `from
ansible.module_utils.facts import *` for this to work as well)
> existing_services = facts.get("services", {})
^ This just returns an empty hash since services does not exist in the dict
returned fro ansible_facts above.
> new_services = do_stuff_with(services)
> module.exit_json(..., ansible_facts={"services" : new_services}
^ When specifying a dictionary this way, the quotes should be left off of
services. I suspect you are losing the dictionary you are passing back because
of the json conversion that exit_json is doing. At this point, the
ansible_facts variable should be set to a python dictionary.
>
>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.).
You will not see the services value from within your module, you are
generating it within your module. The approach I took for returning facts and
persisting data between runs was to use a combination of the module provided
facts and local facts:
http://docs.ansible.com/playbooks_variables.html#local-facts-facts-d
You can see how I'm doing it here:
https://github.com/openshift/openshift-ansible/blob/master/roles/openshift_facts/library/openshift_facts.py
but basically, I'm storing data that I want to persist in
/etc/ansible/openshift.fact and then using that file to determine what "facts"
to return based on the local facts, base defaults, and provider specified
defaults.
Changing line 71 to: 'ansible_facts={services: nix.services},' should be all
that you need. You shouldn't need to import from ansible.module_utils.facts
Hope that helps,
Jason DeTiberus