callback and facts

Hello,

I’m trying to tune ansible in order to :

  1. Connect to a list of Cisco devices (switches + router) and pull facts from the devices (S/N, interfaces,…)

  2. Create a report with each fact’s devices in a CSV file
    For the 1st point I’m currently writing a new module based on Exscript that connects to the device, issue some show commands, parse the output and create some facts based on those outputs.

For the 2nd point I was thinking of a callback plugin using the on_stats methods. Indeed the on_stats method is called at the end of the playbook.

Unfortunately, I don’t understand from the ansible code how facts are created and where in the playbook they are avalaible.

Is it possible to retrieve facts at the end of the playbook for all hosts reachable ?

Thanks for you help :slight_smile:

You should be able to do something like:

for host in self.playbook.inventory.get_hosts():
facts = host.get_variables()

Do something with facts

Thanks for your answer ! It works great for variables but as far as I tested it doesn’t get facts out.
I ran a setup module which pull down a lot of fact from a linux box but those facts are not in get_variables().

Nevertheless I will investigate on this little issue.

Ah, in that case, something like this may be what you want:

hostvars = utils.merge_hash(self.playbook.SETUP_CACHE.copy(), self.playbook.VARS_CACHE)

That will also require import ansible.utils:

from ansible import utils

I’d strongly suggest a template instead of a callback, it would be a lot cleaner.

Hello michael,

What do you mean about a template ? what is it really and how to use it ?

Thanks in advance :slight_smile:

Just dumping a templated file to the local filesystem that contains what you want to see, and saying “go read output.txt” for the results, versus writing a custom callback plugin.

Thanks for the answer michael.

One more question regarding callback. We are currently looking to developp different callback in order to stock facts/vars in CSV & in database. This means we will create 2 callbacks.
I’d like to know if there is any way to enable one/several callback from a playbook variable ? Does Ansible ship with such feature ?

An example will be that all callbacks will have their disabled attribute set to False, then the playbook could read a specific attribute and activate only specific callback based on this attribute.

Thanks for you support.

Pretty much what you said…

Your callback could decide to set a “self.active = True/False” in the constructor, and each method could return “if not self.active” to basically no-op.

Thank fort the answer. My question was how to set the active variable to true/false based on a variable define in the yaml playbook?

The callback interface isn’t really going to see much of those variables directly, unless you jump through some hoops that may not be part of a stable API.

You might want to consider making it learn from an environment variable, depending on the use case.