Aggregating and analyzing gathered facts

insert a play on localhost to run a command that does analysis and returns facts that subsequent plays can use

You would write a custom fact module for this and call it like so:

tasks:

  • action: my_custom_facts_module

It just needs to return a dictionary of elements inside a JSON value called “ansible_facts”.

run a local_action with serialization 1 to cumulate facts into text file (e.g. “map”), then run an idempotent local_action to “reduce” that into a new fact

Hmm, that’s a bit new to me :slight_smile:

You can definitely access facts for all nodes via hostvars and can get the list of all hosts in each group through groups:

http://ansible.cc/docs/playbooks2.html#magic-variables-and-how-to-access-information-about-other-hosts

This would allow you to use the template module to do more or less what you want, I think.
You shouldn’t use local action though, but instead start a new play that only targets localhost.

It will have access to facts from previous plays.

local_action is for doing something locally inside the hosts loop, here you just want to talk to one host.

Related – how can I pass all the gathered host variables as input to a local_action program? For example, if I write a module, how does the module access variables? I understand how modules get the arguments from the argument file, but what if I want more than a few key/value pairs. What if I want a chunk of structured data instead? (E.g the gathered facts or a subset of them.)

I’d generate an input file with template in this case. Modules only have access to variables you explicitly pass to them.

Hope this helps for starter info and where to look, if not, let me know what I can try to explain further!

So a new play on localhost with a template action to create the input file and then a custom module that gets the inputfile name as an input key and returns a data structure. That makes sense.

I would ask that you consider a future feature for a more streamlined way to get facts into modules.

David