How do you write an ansible callback plugin to only show messages from something i explicitly write out like through debug?

188 views
Skip to first unread message

Anders Swenson

unread,
Jul 30, 2019, 9:15:24 AM7/30/19
to Ansible Development
I've made a few attempts but nothing gets rid of all the extra stuff printed to stdout.

I would like only things i explicitly try to print out like through debug to show. 

Here is a simple example: hw.yaml:
===================================
---

- name: run the playbook tasks on the localhost
  hosts: 127.0.0.1
  connection: local
  tasks:
  - name: Hello World
    debug:
      msg: "Hello World!"
===================================

The above prints the following to stdout
===================================

PLAY [run the playbook tasks on the localhost] *********************************

TASK [Gathering Facts] *********************************************************
ok: [127.0.0.1]

TASK [Hello World] *************************************************************
ok: [127.0.0.1] => {
    "msg": "Hello World!"
}

PLAY RECAP *********************************************************************
127.0.0.1                  : ok=2    changed=0    unreachable=0    failed=0
===================================

What i want to see

===================================
ok: [127.0.0.1] => {
    "msg": "Hello World!"
}
===================================
OR just
"msg": "Hello World!"

What i tried:
===================================
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type

from ansible.plugins.callback.default import CallbackModule as CallbackModule_default

class CallbackModule(CallbackModule_default):

    '''
    This is the default callback interface, which simply prints messages
    to stdout when new callback events are received.
    '''

    CALLBACK_VERSION = 2.0
    CALLBACK_TYPE = 'stdout'
    CALLBACK_NAME = 'quite'

    def __init__(self):
        super(CallbackModule, self).__init__()

    def v2_playbook_on_start(self, playbook):
        pass

    def v2_playbook_on_notify(self, handler, host):
        pass

    def v2_playbook_on_no_hosts_matched(self):
        pass

    def v2_playbook_on_no_hosts_remaining(self):
        pass

    def v2_playbook_on_task_start(self, task, is_conditional):
        pass

    def v2_playbook_on_cleanup_task_start(self, task):
        pass

    def v2_playbook_on_handler_task_start(self, task):
        pass

    def v2_playbook_on_vars_prompt(self, varname, private=True, prompt=None, encrypt=None, confirm=False, salt_size=None, salt=None, default=None, unsafe=None):
        pass

    def v2_playbook_on_import_for_host(self, result, imported_file):
        pass

    def v2_playbook_on_not_import_for_host(self, result, missing_file):
        pass

    def v2_playbook_on_stats(self, stats):
        pass

    def v2_playbook_on_include(self, included_file):
        pass

#    def v2_playbook_on_play_start(self, play):
#        pass
===================================

Now this is what i see:
===================================

PLAY [run the playbook tasks on the localhost] *********************************
ok: [127.0.0.1]
ok: [127.0.0.1] => {
    "msg": "Hello World!"
}
===================================

So i almost got it but then when i un-comment out those lines at the bottom i get errors. I'm not sure how to get rid of the PLAY text or the first ok text.

Also if i try something more advanced i see it will print out more stuff
(this script basically takes two params one for how many loops and one for how long to wait before looping:
===================================

PLAY [all] *********************************************************************
ok: [10.40.11.240]
ok: [10.40.11.240] => {
    "msg": "variable foo is 2 and len is 1"
}

PLAY [all] *********************************************************************
ok: [10.40.11.240]
ok: [10.40.11.240] => {
    "msg": "Starting loop number 1"
}
Pausing for 1 seconds
(ctrl+C then 'C' = continue early, ctrl+C then 'A' = abort)
ok: [10.40.11.240]
ok: [10.40.11.240] => {
    "msg": "Starting loop number 2"
}
Pausing for 1 seconds
(ctrl+C then 'C' = continue early, ctrl+C then 'A' = abort)
ok: [10.40.11.240]
===================================

As you can see i get a bunch of extra data i don't want still. The only things i wanted to see where the msgs...

Thanks!

Reply all
Reply to author
Forward
0 new messages