43fdw 发表于 2014-12-1 08:55:45

ansible callbacks

ansible callbacks
编辑callbacks.py脚本文件(/usr/lib/python2.6/site-packages/ansible-1.8-py2.6.egg/ansible/callbacks.py)
找到类AggregateStats
在AggregateStats中有2个方法,


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
    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 ] or ['rc' in value and value['rc'] != 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().values()['stdout'],
            haha      = lazy_out.get('contacted').get(host).get('stdout')
      )




其中的runner_results中有我们想要的输出结果。
使用global将lazy_out设置为runner_results
然后在summarize方法中调用lazy_out,使用字典的get方法进行取值。
值得一提的是,如果你的playbook里面有多个task,sumarize只返回最后一个。

如下:


1
2
3
4
5
6
7
8
9
10
11
12
13
In : results.run()
PLAY *****************************************************************
GATHERING FACTS ***************************************************************
ok:
TASK: ******************************************************
changed:
Out:
{'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}}



页: [1]
查看完整版本: ansible callbacks