As we all know, Ansible's playbook will not show the standard output.
How can you make ansible like commands line with standard output?
Ansible boss+merchant+web-m shell-a "Hostname;ip a" 192.168.6.210 | Success | Rc=0 >>pay-boss+merchant+web1:lo: <LOOPBACK,UP,LOWER_UP> MTU 16436 qdisc noqueue link/loopback 00:00:00:0 0:00:00 BRD 00:00:00:00:00:00 inet 127.0.0.1/8 Scope host Lo2:eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> MTU Q Disc pfifo_fast Qlen 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
Let's try it with playbook
[email protected]:/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 [email protected]:/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
By visibly seeing only return OK failed to return failed
We can get playbook back to stdout by callbacks Pulgin.
How to do it?
First confirm the path of the ANSIBLE.CFG, Pip installs the ansible is the default not ansible.cfg this file, should be so, we go to GitHub to download this configuration file to local,
[Email protected]:/etc/ansible# Tree ├──ansible.cfg├──callbacks│├──jastme.py│└──jastme.pyc├──hosts└──playbook S├──BOSS.YML└──FTP.YML2 directories, 6 files[email protected]:/etc/ansible# pwd/etc/ansible
Modify the ansible.cfg so that the callbacks path is located under your own path
#callback_plugins =/usr/share/ansible_plugins/callback_pluginscallback_plugins =/etc/ansible/callbacks
Write
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 (): P Rint res[' state '] if ' invocation ' in Res.keys (): Print res[' invocation ']
In fact, this callbacks is very simple, do not understand the friend print res can
Let's take a look at the effect
[email protected]:/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 forever2: 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
As a result, the calling method, the state has output, is not very OK? This is more white what you are doing.
Ansible Playbook Callbacks