首页 > 代码库 > ansible callbacks
ansible callbacks
ansible callbacks
编辑callbacks.py脚本文件(/usr/lib/python2.6/site-packages/ansible-1.8-py2.6.egg/ansible/callbacks.py)
找到类AggregateStats
在AggregateStats中有2个方法,
def compute(self, runner_results, setup=False, poll=False, ignore_errors=False): ‘‘‘ walk through all results and increment stats ‘‘‘ for (host, value) in runner_results.get(‘contacted‘, {}).iteritems(): if not ignore_errors and ((‘failed‘ in value and bool(value[‘failed‘])) or (‘failed_when_result‘ in value and [value[‘failed_when_result‘]] or [‘rc‘ in value and value[‘rc‘] != 0])[0]): self._increment(‘failures‘, host) elif ‘skipped‘ in value and bool(value[‘skipped‘]): self._increment(‘skipped‘, host) elif ‘changed‘ in value and bool(value[‘changed‘]): if not setup and not poll: self._increment(‘changed‘, host) self._increment(‘ok‘, host) else: if not poll or (‘finished‘ in value and bool(value[‘finished‘])): self._increment(‘ok‘, host) for (host, value) in runner_results.get(‘dark‘, {}).iteritems(): self._increment(‘dark‘, host) global lazy_out lazy_out = runner_results print lazy_out.get(‘contacted‘).get(host).get(‘stdout‘) def summarize(self, host): ‘‘‘ return information about a particular host ‘‘‘ return dict( ok = self.ok.get(host, 0), failures = self.failures.get(host, 0), unreachable = self.dark.get(host,0), changed = self.changed.get(host, 0), skipped = self.skipped.get(host, 0), #haha = lazy_out.values()[1].values()[0][‘stdout‘], haha = lazy_out.get(‘contacted‘).get(host).get(‘stdout‘) )
其中的runner_results中有我们想要的输出结果。
使用global将lazy_out设置为runner_results
然后在summarize方法中调用lazy_out,使用字典的get方法进行取值。
值得一提的是,如果你的playbook里面有多个task,sumarize只返回最后一个。
如下:
In [10]: results.run() PLAY [test] ***************************************************************** GATHERING FACTS *************************************************************** ok: [test] TASK: [test] ****************************************************** changed: [test] Out[10]: {‘test‘: {‘changed‘: 1, ‘failures‘: 0, ‘haha‘: u‘1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN \n link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00\n inet 127.0.0.1/8 scope host lo\n inet6 ::1/128 scope host \n valid_lft forever preferred_lft forever\n2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000\n link/ether fa:16:3e:7a:b4:08 brd ff:ff:ff:ff:ff:ff\n inet 192.168.10.12/24 brd 192.168.10.255 scope global eth0\n inet6 fe80::f816:3eff:fe7a:b408/64 scope link \n valid_lft forever preferred_lft forever‘, ‘ok‘: 2, ‘skipped‘: 0, ‘unreachable‘: 0}}
ansible callbacks
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。