Collecting and evaluating generic values from multiple hosts

Hi,

I have a simple (generic Python) script which sshs into multiple machines, executes a command and then does some calculations on the returned stdout, eventually printing a summary from the dict { hostname: stdout, … }.
I’d like to take advantage of Ansible’s connection mechanisms such as integrated password handling and having multiple connections at the same time. Is there an easy way to integrate such a script with Ansible or is it possible to write a module which receives the { host: result } dict after all hosts have been queried?

Cheers
/rike

Sure thing!

My recommendation is probably to look at the lib/ansible/runner/init.py API, as used by /usr/bin/ansible.

The final call made there gets back exactly that dictionary, and you can instrument your own custom callbacks class if you want any intermediate feedback (or use the one supplied).

–Michael

Ah, that’s great. Thanks for your answer.

So the way to go would be to write a Python script using the Ansible API to do the wiring between callback and result, am I correct? And also, I’d have to employ my own argparser (or re-use the Cli class somehow) in order to select the hosts I want to query.
There is no way to use the bin/ansible or a playbook to do this, where I would specify the callback which should receive the data, or is it? I have to do it programmatically and put the callback into its dedicated folder? (We just run Ansible from the git repo when we need it, so we’re currently not having the proper folder infrastructure set up.)

Hmm, on the other hand, I guess, I could just make use of the --tree argument and read it from there as well. Seems even simpler in a way.

Thanks again
/rike

I think it might help if I understood the use case of what you are trying to do further.

As I understood it you just wanted the results for each host, which is what you get from the run call, and you don’t need a callback to get that data. However, callbacks are useful if you want to control various output.

It seems like if you just want the result of running something you could basically ignore the callbacks.

Currently, I’m just having a small script to query multiple hosts for installed packages and then print a list of packages which are missing on some hosts. Nothing especially fancy. I’m planning to add some other querying tools as well as the need arises but basically, they should all work rather similarly and indeed I wouldn’t need any particular callbacks to accomplish this.

I think I’ll really just go the --tree route for fetching the data and then simply use a JSON parser on the files in that directory. That seems to be all I need at the moment.

/rike