Understanding callbacks

Hello,

I'm trying to grasp callbacks with an example: I'd like to gather the
output of the setup module invoked by a Playbook. I've created a copy
of noop.py...

        class CallbackModule(object):

            def runner_on_ok(self, host, res):
                print ">>>>>>>>>>> dump"
                if res.get("verbose_override", None) is not None:
                    fname = "/tmp/a.%s" % host
                    print "++---- dumping to %s" % fname
                    f = open(fname, 'w')
                    f.write(json.dumps(res))
                    f.close()

                print "JP: runner_on_ok: %s" % host
                pass

            def playbook_on_task_start(self, name, is_conditional):
                print "JP: Playbook TASK %s" % name
                pass

            def playbook_on_setup(self):
                print "JP: ONSETUP"
                pass

1. I note that _on_setup is invoked just before a playbook gathers facts.
2. I see runner_on_ok is invoked AFTER setup has gathered the facts, but
   it isn't passed the name of the task; all successfully completed
   tasks end up here. Is there any way to find the name of the task?

Thanks,

        -JP

Hello,

I'm trying to grasp callbacks with an example: I'd like to gather the
output of the setup module invoked by a Playbook. I've created a copy
of noop.py...

        class CallbackModule(object):

            def runner_on_ok(self, host, res):
                print ">>>>>>>>>>> dump"
                if res.get("verbose_override", None) is not None:
                    fname = "/tmp/a.%s" % host
                    print "++---- dumping to %s" % fname
                    f = open(fname, 'w')
                    f.write(json.dumps(res))
                    f.close()

                print "JP: runner_on_ok: %s" % host
                pass

            def playbook_on_task_start(self, name, is_conditional):
                print "JP: Playbook TASK %s" % name
                pass

            def playbook_on_setup(self):
                print "JP: ONSETUP"
                pass

1. I note that _on_setup is invoked just before a playbook gathers facts.
2. I see runner_on_ok is invoked AFTER setup has gathered the facts, but
   it isn't passed the name of the task; all successfully completed
   tasks end up here. Is there any way to find the name of the task?

Yes. You should see the 'invocation' parameters in the JSON that
indicate what the module and parameters were.

These are hidden from regular /usr/bin/ansible-playbook output, but
available to API callers.

You may also wish to see the demo logging callback in
https://github.com/ansible/ansible-plugins/blob/master/callbacks/log_plays.py
which does exactly this.

Michael,

You may also wish to see the demo logging callback in
https://github.com/ansible/ansible-plugins/blob/master/callbacks/log_plays.py
which does exactly this.

that helps, thank you, but:

Yes. You should see the 'invocation' parameters in the JSON that
indicate what the module and parameters were.

I see no mention of "invocation" in the JSON (might well be thick-headed
at the moment, though): the shortened version of your callback (below) shows

        ****** INVOCATION [None]
and [ print(data) ] and entry to log() has no "invocation" object in
it...

What am I missing?

        -JP

        import os
        import time
        import json

        TIME_FORMAT="%b %d %Y %H:%M:%S"
        MSG_FORMAT="TIME: %(now)s - CATEG: %(category)s - DATA: %(data)s\n\n"

        if not os.path.exists("/tmp/log/ansible/hosts"):
            os.makedirs("/tmp/log/ansible/hosts")

        def log(host, category, data):
            if type(data) == dict:
                invocation = data.pop('invocation', None)
                print "****** INVOCATION [%s]" % invocation
                data = json.dumps(data)

            path = os.path.join("/tmp/log/ansible/hosts", host)
            now = time.strftime(TIME_FORMAT, time.localtime())
            fd = open(path, "a")
            fd.write(MSG_FORMAT % dict(now=now, category=category, data=data))
            fd.close()

        class CallbackModule(object):
            def runner_on_ok(self, host, res):
                log(host, 'OK', res)