ansible2.6 callback

cat ansible.cfg

callback_whitelist = timer, mail,black_hole
callback_plugins   = /usr/share/ansible/plugins/callback
# by default callbacks are not loaded for /bin/ansible, enable this if you
# want, for example, a notification or logging callback to also apply to
bin_ansible_callbacks = True

cat /usr/share/ansible/plugins/callback/black_hole.py

cat black_hole.py
import sys
import commands
import MySQLdb
import os
import json
from collections import namedtuple
#from ansible.inventory import Inventory
#from ansible.vars import VariableManager
from ansible.parsing.dataloader import DataLoader
from ansible.executor.playbook_executor import PlaybookExecutor
from ansible.plugins.callback import CallbackBase
from ansible.errors import AnsibleParserError

from ansible.plugins.callback import CallbackBase

class CallbackModule(CallbackBase):
    def v2_runner_on_ok(self, result):
        host = result._host.get_name()
        f = open('/tmp/result_callback', 'a+')
        f.write(str(host) + "aaaaaa\n")
        f.write(str(result._result['stdout_lines']))
        f.close()
    def v2_runner_on_unreachable(self, result):
        host = result._host.get_name()
        f = open('/tmp/result_callback_unreachable', 'a+')
        f.write(str(host) + "aaaaaa\n")
        f.write(str(result._result['stdout_lines']))
        f.close()
    def v2_runner_on_failed(self, result, ignore_errors=False):
        host = result._host.get_name()
        f = open('/tmp/result_callback_failed', 'a+')
        f.write(str(host) + "aaaaaa\n")
        f.write(str(result._result['stdout_lines']))
        f.close()
[root@localhost tmp]# cat result_callback_unreachable 
192.168.1.103aaaaaa
[root@localhost tmp]# 
[root@localhost tmp]# ls
ks-script-Bfkpe0             systemd-private-270df7a5c187446a9259969e2d81e835-chronyd.service-FhawaX   yum.log
result_callback              systemd-private-270df7a5c187446a9259969e2d81e835-vgauthd.service-Ba59mJ
result_callback_unreachable  systemd-private-270df7a5c187446a9259969e2d81e835-vmtoolsd.service-jYl8Av
[root@localhost tmp]# cat result_callback
192.168.1.102aaaaaa
[u' 22:17:08 up  6:47,  4 users,  load average: 0.00, 0.01, 0.05'][root@localhost tmp]#

转载于:https://blog.51cto.com/szgb17/2175450

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值