Callback-Plugins seem to have one instance per fork - how to synchronize them?

Hi,

we have a callback plugin (Ansible 1.8.4) which gathers information during the playbook run and stores them in a class-local variable.
On playbook end this should be displayed in the log (e.g. warnings and errors found)

Here is the relevant code:

class CallbackModule(object):

saved_logs = {}

def runner_on_failed(self, host, res, ignore_errors=False):
… put something in saved_logs

def runner_on_ok(self, host, res):
… put something in saved_logs

def runner_on_error(self, host, msg):
… put something in saved_logs

def runner_on_skipped(self, host, item=None):
… put something in saved_logs

def playbook_on_stats(self, stats):
… display contents of saved_logs

Now it seems that the Callback gets one instance per fork as we only see in stats the content which was added by tasks running on the controller. Logs generated e.g. in modules running on the nodes are only available in stats if “fork” is set to 1

Is this behaviour intentional?

How should we fix/synchronize this?

Regards Michael

I think the problem is that you are using a class attribute for saved_logs as opposed to an instance attribute.

An instance attribute would be instatiated in init() and because Ansible instantiates the class 1 time and passes that instance to all forks you shouldn’t have that problem.

Hi,

but we need to have a class attribute - I do not want one variable per instance but one Variable which holds all logs of all forks.

So a class-variable should be the right one. Unfortunately it seems that the instance gets copied for every fork and is not synchronized back.

Regards Michael