ansible的callback 插件有点类似saltsatck的returners,可以收集playbook运行过程中一些信息,这里我把执行结果写入到mongodb中。
ansible 几个demo callbacks 的git地址
https://github.com/ansible/ansible/tree/devel/plugins/callbacks
根据官方的日志写文件的脚本改了下,当然你也可以写mysq redis,我下面就对失败和成功的做了写入,你根据自己的情况可以对其他状态做处理。(python刚学,有写的比较搓的地方,请指教)
这里有峰云写的入redis的 http://rfyiamcool.blog.51cto.com/1030776/1440624
还有灿哥写的执行任务后发邮件的 ansible-插件之callback_plugins
这个脚本放在/usr/share/ansible_plugins/callback_plugins 目录下,给执行权限,
这个目录是在ansible.cfg中定义的
# set plugin path directories here, separate with colons action_plugins = /usr/share/ansible_plugins/action_plugins callback_plugins = /usr/share/ansible_plugins/callback_plugins connection_plugins = /usr/share/ansible_plugins/connection_plugins lookup_plugins = /usr/share/ansible_plugins/lookup_plugins vars_plugins = /usr/share/ansible_plugins/vars_plugins filter_plugins = /usr/share/ansible_plugins/filter_plugins
可以看到里面还有其他插件,有时间都试试
下面是python脚本
import os import time import json import datetime from pymongo import MongoClient mongoinfo = {"host":"127.0.0.1","port":"27017","user":"ops","password":"ops","dbname":"ansible_log"} TIME_FORMAT='%Y-%m-%d %H:%M:%S' def InsertDB(values): global mongoinfo dbhost = mongoinfo['host'] dbport = mongoinfo['port'] dbuser = mongoinfo['user'] dbpwd = mongoinfo['password'] dbname = mongoinfo['dbname'] uri = 'mongodb://%s:%s@%s/%s'%(dbuser,dbpwd,dbhost,dbname) client = MongoClient(uri,safe=False) db = client.ansible_log db.callback.insert(values) class CallbackModule(object): def on_any(self, *args, **kwargs): pass def runner_on_failed(self, host, res, ignore_errors=False): now = datetime.datetime.now() result = res result['time'] = now.strftime(TIME_FORMAT) result['status'] = 'fail' InsertDB(result) def runner_on_ok(self, host, res): now = datetime.datetime.now() result = res result['time'] = now.strftime(TIME_FORMAT) result['status'] = 'ok' InsertDB(result) def runner_on_skipped(self, host, item=None): pass def runner_on_unreachable(self, host, res): now = datetime.datetime.now() result = res result['time'] = now.strftime(TIME_FORMAT) result['status'] = 'unreachable' InsertDB(result) def runner_on_no_hosts(self): pass def runner_on_async_poll(self, host, res, jid, clock): pass def runner_on_async_ok(self, host, res, jid): pass def runner_on_async_failed(self, host, res, jid): pass def playbook_on_start(self): pass def playbook_on_notify(self, host, handler): pass def playbook_on_no_hosts_matched(self): pass def playbook_on_no_hosts_remaining(self): pass def playbook_on_task_start(self, name, is_conditional): pass def playbook_on_vars_prompt(self, varname, private=True, prompt=None, encrypt=None, confirm=False, salt_size=None, salt=None, default=None): pass def playbook_on_setup(self): pass def playbook_on_import_for_host(self, host, imported_file): pass def playbook_on_not_import_for_host(self, host, missing_file): pass def playbook_on_play_start(self, name): pass def playbook_on_stats(self, stats): pass
查看mongodb中数据