标签:
大家都知道ansible的playbook是不会显示标准输出的。
如何能让ansible像commands line 一样有标准输出呢?
ansible Boss+Merchant+Web -m shell -a "hostname;ip a" 192.168.6.210 | success | rc=0 >> Pay-Boss+Merchant+Web 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000 link/ether 00:0c:29:96:06:4e brd ff:ff:ff:ff:ff:ff inet 192.168.6.210/24 brd 192.168.6.255 scope global eth0
我们使用playbook试试呢
root@ProFtp:/etc/ansible# ansible-playbook playbooks/ftp.yml PLAY [Ftp] ******************************************************************** GATHERING FACTS *************************************************************** ok: [192.168.6.11] TASK: [Make Sure ProFtp is running] ******************************************* ok: [192.168.6.11] TASK: [show hostname] ********************************************************* changed: [192.168.6.11] TASK: [show ip] *************************************************************** changed: [192.168.6.11] PLAY RECAP ******************************************************************** 192.168.6.11 : ok=4 changed=2 unreachable=0 failed=0 root@ProFtp:/etc/ansible# cat playbooks/ftp.yml - hosts : Ftp remote_user : root tasks : - name : Make Sure ProFtp is running service: name=proftpd state=running - name : show hostname shell : cat /etc/issue || /bin/true - name : show ip command : ip a
通过上图明显看到只会返回OK 失败 返回FAILED
我们可以通过callbacks pulgin 来让playbook返回stdout
如何做呢?
首先确认ansible.cfg的路径,pip 安装的ansible是默认没有ansible.cfg这个文件,应该是这样,我们到github上下载这个配置文件到本地,
root@ProFtp:/etc/ansible# tree . ├── ansible.cfg ├── callbacks │ ├── jastme.py │ └── jastme.pyc ├── hosts └── playbooks ├── boss.yml └── ftp.yml 2 directories, 6 files root@ProFtp:/etc/ansible# pwd /etc/ansible
修改ansible.cfg,让callbacks的路径定位到你自己的路径下
#callback_plugins = /usr/share/ansible_plugins/callback_plugins callback_plugins = /etc/ansible/callbacks
编写
class CallbackModule(object): #if foo: # self.disabled = True pass def runner_on_ok(self, host, res): # pass if ‘stdout‘ in res.keys(): print res[‘stdout‘] if ‘state‘ in res.keys(): print res[‘state‘] if ‘invocation‘ in res.keys(): print res[‘invocation‘]
其实这个callbacks很简单,不明白的朋友打印一下res即可
我们来看看效果
root@ProFtp:/etc/ansible# ansible-playbook playbooks/ftp.yml PLAY [Ftp] ******************************************************************** GATHERING FACTS *************************************************************** ok: [192.168.6.11] {‘module_name‘: ‘setup‘, ‘module_args‘: ‘‘} TASK: [Make Sure ProFtp is running] ******************************************* ok: [192.168.6.11] started {‘module_name‘: u‘service‘, ‘module_args‘: u‘name=proftpd state=running‘} TASK: [show hostname] ********************************************************* changed: [192.168.6.11] Ubuntu 14.04.2 LTS \n \l {‘module_name‘: u‘shell‘, ‘module_args‘: u‘cat /etc/issue || /bin/true‘} TASK: [show ip] *************************************************************** changed: [192.168.6.11] 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 00:0c:29:75:aa:92 brd ff:ff:ff:ff:ff:ff inet 192.168.6.11/24 brd 192.168.6.255 scope global eth0 valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fe75:aa92/64 scope link valid_lft forever preferred_lft forever {‘module_name‘: u‘command‘, ‘module_args‘: u‘ip a‘} PLAY RECAP ******************************************************************** 192.168.6.11 : ok=4 changed=2 unreachable=0 failed=0
结果,调用的方法,状态 都有输出了,是不是很OK?这样就更白自己在做什么了。
标签:
原文地址:http://my.oschina.net/jastme/blog/490269