Playbook支持任務(wù)結(jié)果回顯唱星,不只是執(zhí)行是否成功

ansible1.9.1版本
寫(xiě)callback插件功能實(shí)現(xiàn)

1.ansible.cfg配置文件(變更c(diǎn)allback路徑位置)
[defaults]
host_key_checking   = False
callback_plugins = /tmp/callback
2.編寫(xiě)回顯callback腳本

cc.py文件名字可以自定義,但是里面的內(nèi)容有具體要求

vim /tmp/callback/cc.py
#!/usr/bin/env python
# -*- coding=utf-8 -*-
######################
class CallbackModule(object):
    #執(zhí)行成功的
    def runner_on_ok(self, host, res):
    #print res
        if 'stdout' in res.keys():
            print res['']
        if 'state' in res.keys():
            print res['state']
        if 'invocation' in res.keys():
            print res['invocation']
    #執(zhí)行失敗的
    def runner_on_failed(self, host, res, ignore_errors=False):
        pass
    #執(zhí)行跳過(guò)的
    def runner_on_skipped(self, host, item=None):
        pass
    #主機(jī)不可達(dá)的
    def runner_on_unreachable(self, host, res):
        pass

是不是很簡(jiǎn)單,按照標(biāo)注文件定義CallbackModule類(lèi),重寫(xiě)里面的runner_on_ok和runner_on_failed方法就好了,后面接的host,res,以及其他都是固定寫(xiě)法寥院,不能

3.編寫(xiě)yaml文件

/tmp/test.yml

---
- hosts: all
  tasks:
  - name: test playbook
    shell: 'df -h'

/tmp/hosts

[task]
127.0.0.1
4.執(zhí)行playbook
cd /tmp/
ansible-playbook -i hosts all test.yml -k
SSH password: 

PLAY [all] ******************************************************************** 

GATHERING FACTS *************************************************************** 
ok: [127.0.0.1]
{'module_name': 'setup', 'module_args': ''}

TASK: [test playbook] ********************************************************* 
changed: [127.0.0.1]
Filesystem                      Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root    6.7G  5.0G  1.4G  80% /
tmpfs                           499M   16K  499M   1% /dev/shm
/dev/sda1                       485M   34M  426M   8% /boot
Python_20160906_20161122_15_16  466G   30G  436G   7% /media/sf_Python_20160906_20161122_15_16
Python_20160906_20161122_15_16  6.7G  5.0G  1.4G  80% /export//App/DevOPS/
/dev/sr0                        4.2G  4.2G     0 100% /mnt/cdrom
{'module_name': u'shell', 'module_args': u'df -h'}

PLAY RECAP ******************************************************************** 
127.0.0.1                  : ok=2    changed=1    unreachable=0    failed=0   

5.callback里面的插件host和res是固定寫(xiě)法,不能改

嘗試print res看下里面的內(nèi)容,大字典里面各種信息


{
    "invocation": {
        "module_name": "setup", 
        "module_args": ""
    }, 
    "verbose_override": true, 
    "changed": false, 
    "ansible_facts": {
        "ansible_product_serial": "0", 
        "ansible_form_factor": "Other", 
        "ansible_product_version": "1.2", 
        "ansible_fips": false, 
        "ansible_swaptotal_mb": 815, 
        "ansible_user_id": "root", 
        "module_setup": true, 
        "ansible_userspace_bits": "64", 
        "ansible_distribution_version": "6.5", 
        "ansible_domain": "", 
        "ansible_virtualization_type": "virtualbox", 
        "ansible_python_version": "2.7.12", 
        "ansible_processor_cores": 1, 
        "ansible_virtualization_role": "guest", 
        "ansible_env": {
            "LC_CTYPE": "en_US.UTF-8", 
            "LESSOPEN": "|/usr/bin/lesspipe.sh %s", 
            "SSH_CLIENT": "127.0.0.1 58871 22", 
            "CVS_RSH": "ssh", 
            "LOGNAME": "root", 
            "USER": "root", 
            "HOME": "/root", 
            "PATH": "/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin", 
            "_": "/usr/bin/python", 
            "LANG": "en_US.UTF-8", 
            "QTLIB": "/usr/lib64/qt-3.3/lib", 
            "SHELL": "/bin/bash", 
            "SHLVL": "2", 
            "G_BROKEN_FILENAMES": "1", 
            "QTINC": "/usr/lib64/qt-3.3/include", 
            "PWD": "/root", 
            "MAIL": "/var/mail/root", 
            "SSH_CONNECTION": "127.0.0.1 58871 127.0.0.1 22", 
            "QTDIR": "/usr/lib64/qt-3.3"
        }, 
        "ansible_processor_vcpus": 1, 
        "ansible_bios_version": "VirtualBox", 
        "ansible_processor": [
            "GenuineIntel", 
            "Intel(R) Core(TM) i5-4590 CPU @ 3.30GHz"
        ], 
        "ansible_date_time": {
            "tz": "HKT", 
            "hour": "13", 
            "time": "13:53:08", 
            "tz_offset": "+0800", 
            "month": "01", 
            "epoch": "1484891588", 
            "iso8601_micro": "2017-01-20T05:53:08.288573Z", 
            "weekday": "Friday", 
            "year": "2017", 
            "date": "2017-01-20", 
            "iso8601": "2017-01-20T05:53:08Z", 
            "day": "20", 
            "minute": "53", 
            "second": "08"
        }, 
        "ansible_lo": {
            "active": true, 
            "promisc": false, 
            "ipv4": {
                "netmask": "255.0.0.0", 
                "network": "127.0.0.0", 
                "address": "127.0.0.1"
            }, 
            "ipv6": [
                {
                    "scope": "host", 
                    "prefix": "128", 
                    "address": "::1"
                }
            ], 
            "device": "lo", 
            "type": "loopback", 
            "mtu": 16436
        }, 
        "ansible_memtotal_mb": 996, 
        "ansible_architecture": "x86_64", 
        "ansible_default_ipv4": {
            "macaddress": "08:00:27:9e:81:b6", 
            "network": "10.0.3.0", 
            "mtu": 1500, 
            "alias": "eth1", 
            "netmask": "255.255.255.0", 
            "address": "10.0.3.15", 
            "interface": "eth1", 
            "type": "ether", 
            "gateway": "10.0.3.2"
        }, 
        "ansible_swapfree_mb": 767, 
        "ansible_default_ipv6": {}, 
        "ansible_distribution_release": "Final", 
        "ansible_system_vendor": "innotek GmbH", 
        "ansible_os_family": "RedHat", 
        "ansible_cmdline": {
            "LANG": "en_US.UTF-8", 
            "rd_NO_LUKS": true, 
            "SYSFONT": "latarcyrheb-sun16", 
            "KEYBOARDTYPE": "pc", 
            "rd_NO_MD": true, 
            "quiet": true, 
            "rd_LVM_LV": "VolGroup/lv_root", 
            "rhgb": true, 
            "KEYTABLE": "us", 
            "ro": true, 
            "root": "/dev/mapper/VolGroup-lv_root", 
            "rd_NO_DM": true
        }, 
        "ansible_user_gid": 0, 
        "ansible_selinux": false, 
        "ansible_userspace_architecture": "x86_64", 
        "ansible_product_uuid": "89D6BCED-A20D-4FB9-B589-9C5DD464E025", 
        "ansible_system": "Linux", 
        "ansible_pkg_mgr": "yum", 
        "ansible_memfree_mb": 71, 
        "ansible_devices": {
            "sr0": {
                "scheduler_mode": "cfq", 
                "rotational": "1", 
                "vendor": "VBOX", 
                "sectors": "8726528", 
                "host": "IDE interface: Intel Corporation 82371AB/EB/MB PIIX4 IDE (rev 01)", 
                "sectorsize": "2048", 
                "removable": "1", 
                "support_discard": "0", 
                "model": "CD-ROM", 
                "size": "16.64 GB", 
                "holders": [], 
                "partitions": {}
            }, 
            "sda": {
                "scheduler_mode": "cfq", 
                "rotational": "1", 
                "vendor": "ATA", 
                "sectors": "16777216", 
                "host": "SATA controller: Intel Corporation 82801HM/HEM (ICH8M/ICH8M-E) SATA Controller [AHCI mode] (rev 02)", 
                "sectorsize": "512", 
                "removable": "0", 
                "support_discard": "0", 
                "model": "VBOX HARDDISK", 
                "size": "8.00 GB", 
                "holders": [], 
                "partitions": {
                    "sda2": {
                        "start": "1026048", 
                        "sectorsize": 512, 
                        "sectors": "15751168", 
                        "size": "7.51 GB"
                    }, 
                    "sda1": {
                        "start": "2048", 
                        "sectorsize": 512, 
                        "sectors": "1024000", 
                        "size": "500.00 MB"
                    }
                }
            }
        }, 
        "ansible_user_uid": 0, 
        "ansible_memory_mb": {
            "real": {
                "total": 996, 
                "free": 71, 
                "used": 925
            }, 
            "swap": {
                "cached": 2, 
                "total": 815, 
                "used": 48, 
                "free": 767
            }, 
            "nocache": {
                "used": 462, 
                "free": 534
            }
        }, 
        "ansible_distribution": "CentOS", 
        "ansible_ssh_host_key_dsa_public": "AAAAB3NzaC1kc3MAAACBAODMPPNUb9UNRIDiaV+x+Fsc6cCssMp7oYowL8oZVfKuNJUp9/mvZ329ufYBjvbu4Cad3e0jFq27XqKPFr09sMMS/DGef4MolDO75ffc+Nu9O+bPaKxjyKmUTKXIuSXy5Eh+TiWDnFERY/cN+7EjQG34ruSORob+SAW8z6htOOqDAAAAFQCo21k9L9/Zi/PUAtys7YbU4GYDjwAAAIA/MIKoJQEhy989MX0T/Cp6ybD+Q1r8w3JSwkY+HkntXNcR9PFxedd3VUYXX/JwS/x5427Xo3g00mDgqSLiWJmCnMjzvOES0wwZH8gEP0Lq9Z0Q/8EKZQDPCNH5ygshtHvOajTOl8ekyrxteJhTKlA1kBYkTX2Puhom149OPKWqwgAAAIBRCmrKWy99Ns7LonFb5dEa2Jmlv4dKHHasD0x7LpaG4Yst3HCx3/uXNOgrWaikn2rt1W4WdLcOshcySvSeXwadisc+g1aYXhHziUvqMfzze8FmOFLjO+zpBim442PcreiXR9H+CLXHeCFBTb2jF3AYjbE9op7wjZ/y3ytx1YjtBw==", 
        "ansible_user_dir": "/root", 
        "ansible_processor_count": 1, 
        "ansible_hostname": "Master", 
        "ansible_lsb": {
            "release": "6.5", 
            "major_release": "6", 
            "codename": "Final", 
            "id": "CentOS", 
            "description": "CentOS release 6.5 (Final)"
        }, 
        "ansible_bios_date": "12/01/2006", 
        "ansible_all_ipv6_addresses": [
            "fe80::a00:27ff:fe49:8f9d", 
            "fe80::a00:27ff:fe9e:81b6"
        ], 
        "ansible_interfaces": [
            "lo", 
            "eth1", 
            "eth0"
        ], 
        "ansible_machine_id": "62880a0571db6d0b07a165dd00000008", 
        "ansible_ssh_host_key_rsa_public": "AAAAB3NzaC1yc2EAAAABIwAAAQEA03lrhLYsaOMezLT5fZvuUtsxC5vLh4KUy3jJ8ArnPwexvNQOwTZSG5rmoKpsgqBRdG5QGJiWQio6HE3IAkhMyIfbrO6mc1TYzCwBcI2bXa1Hw3SuoOvOlLnOv5emD94Y7QPrwqPc+F3fY8o5Je1DMysNRZYJIrGCYkzG3HlcLh0M8UI2HtonzeZ7tj8KjLTfUihKgz61ULhpU4x7ncJiYuhl1/hdHdQywgz2kmNULY4qMAx6RJootznxaaHg/rR2mLrpid0uBM2eAf22q5viPIBMLY3bdSWphdPDs513idkK9E6fQuyujhxc6X3hTKfshvYD3Xz5kz1Z/Vf9XMnRew==", 
        "ansible_machine": "x86_64", 
        "ansible_user_gecos": "root", 
        "ansible_kernel": "2.6.32-431.el6.x86_64", 
        "ansible_processor_threads_per_core": 1, 
        "ansible_fqdn": "Master", 
        "ansible_mounts": [
            {
                "options": "rw", 
                "uuid": "58f238c7-9ae6-4ece-b800-5c103d6a0b19", 
                "size_total": 7092494336, 
                "device": "/dev/mapper/VolGroup-lv_root", 
                "mount": "/", 
                "size_available": 1408520192, 
                "fstype": "ext4"
            }, 
            {
                "options": "rw", 
                "uuid": "8a66244f-c7e5-4627-aca0-67881ea1d19d", 
                "size_total": 507744256, 
                "device": "/dev/sda1", 
                "mount": "/boot", 
                "size_available": 446173184, 
                "fstype": "ext4"
            }, 
            {
                "options": "ro", 
                "uuid": "", 
                "size_total": 4467802112, 
                "device": "/dev/sr0", 
                "mount": "/mnt/cdrom", 
                "size_available": 0, 
                "fstype": "iso9660"
            }
        ], 
        "ansible_eth0": {
            "macaddress": "08:00:27:49:8f:9d", 
            "module": "e1000", 
            "mtu": 1500, 
            "active": true, 
            "promisc": false, 
            "ipv4": {
                "netmask": "255.255.255.0", 
                "network": "192.168.1.0", 
                "address": "192.168.1.2"
            }, 
            "ipv6": [
                {
                    "scope": "link", 
                    "prefix": "64", 
                    "address": "fe80::a00:27ff:fe49:8f9d"
                }
            ], 
            "device": "eth0", 
            "type": "ether"
        }, 
        "ansible_eth1": {
            "macaddress": "08:00:27:9e:81:b6", 
            "module": "e1000", 
            "mtu": 1500, 
            "active": true, 
            "promisc": false, 
            "ipv4": {
                "netmask": "255.255.255.0", 
                "network": "10.0.3.0", 
                "address": "10.0.3.15"
            }, 
            "ipv6": [
                {
                    "scope": "link", 
                    "prefix": "64", 
                    "address": "fe80::a00:27ff:fe9e:81b6"
                }
            ], 
            "device": "eth1", 
            "type": "ether"
        }, 
        "ansible_product_name": "VirtualBox", 
        "ansible_user_shell": "/bin/bash", 
        "ansible_distribution_major_version": "6", 
        "ansible_all_ipv4_addresses": [
            "192.168.1.2", 
            "10.0.3.15"
        ], 
        "ansible_nodename": "Master"
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末涛目,一起剝皮案震驚了整個(gè)濱河市秸谢,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌霹肝,老刑警劉巖估蹄,帶你破解...
    沈念sama閱讀 206,482評(píng)論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異沫换,居然都是意外死亡臭蚁,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,377評(píng)論 2 382
  • 文/潘曉璐 我一進(jìn)店門(mén)讯赏,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)垮兑,“玉大人,你說(shuō)我怎么就攤上這事漱挎∠登梗” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 152,762評(píng)論 0 342
  • 文/不壞的土叔 我叫張陵磕谅,是天一觀(guān)的道長(zhǎng)私爷。 經(jīng)常有香客問(wèn)我,道長(zhǎng)膊夹,這世上最難降的妖魔是什么衬浑? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 55,273評(píng)論 1 279
  • 正文 為了忘掉前任,我火速辦了婚禮放刨,結(jié)果婚禮上工秩,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好助币,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,289評(píng)論 5 373
  • 文/花漫 我一把揭開(kāi)白布浪听。 她就那樣靜靜地躺著,像睡著了一般奠支。 火紅的嫁衣襯著肌膚如雪馋辈。 梳的紋絲不亂的頭發(fā)上抚芦,一...
    開(kāi)封第一講書(shū)人閱讀 49,046評(píng)論 1 285
  • 那天倍谜,我揣著相機(jī)與錄音,去河邊找鬼叉抡。 笑死尔崔,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的褥民。 我是一名探鬼主播季春,決...
    沈念sama閱讀 38,351評(píng)論 3 400
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼消返!你這毒婦竟也來(lái)了载弄?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 36,988評(píng)論 0 259
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤撵颊,失蹤者是張志新(化名)和其女友劉穎宇攻,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體倡勇,經(jīng)...
    沈念sama閱讀 43,476評(píng)論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡逞刷,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 35,948評(píng)論 2 324
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了妻熊。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片夸浅。...
    茶點(diǎn)故事閱讀 38,064評(píng)論 1 333
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖扔役,靈堂內(nèi)的尸體忽然破棺而出帆喇,到底是詐尸還是另有隱情,我是刑警寧澤亿胸,帶...
    沈念sama閱讀 33,712評(píng)論 4 323
  • 正文 年R本政府宣布番枚,位于F島的核電站,受9級(jí)特大地震影響损敷,放射性物質(zhì)發(fā)生泄漏葫笼。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,261評(píng)論 3 307
  • 文/蒙蒙 一拗馒、第九天 我趴在偏房一處隱蔽的房頂上張望路星。 院中可真熱鬧,春花似錦、人聲如沸洋丐。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 30,264評(píng)論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)友绝。三九已至堤尾,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間迁客,已是汗流浹背郭宝。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 31,486評(píng)論 1 262
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留掷漱,地道東北人粘室。 一個(gè)月前我還...
    沈念sama閱讀 45,511評(píng)論 2 354
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像卜范,于是被迫代替她去往敵國(guó)和親衔统。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,802評(píng)論 2 345

推薦閱讀更多精彩內(nèi)容

  • Spring Cloud為開(kāi)發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見(jiàn)模式的工具(例如配置管理海雪,服務(wù)發(fā)現(xiàn)锦爵,斷路器,智...
    卡卡羅2017閱讀 134,599評(píng)論 18 139
  • 個(gè)人入門(mén)學(xué)習(xí)用筆記奥裸、不過(guò)多作為參考依據(jù)险掀。如有錯(cuò)誤歡迎斧正 目錄 簡(jiǎn)書(shū)好像不支持錨點(diǎn)、復(fù)制搜索(反正也是寫(xiě)給我自己看...
    kirito_song閱讀 2,451評(píng)論 1 37
  • callback 是經(jīng)常用到的插件刺彩,而且還是自定義很強(qiáng)的迷郑,在任務(wù)的每個(gè)狀態(tài)下執(zhí)行某些動(dòng)作。 觸發(fā)事件的列表 可以定...
    lework閱讀 5,266評(píng)論 3 1
  • 本文主要內(nèi)容均收集于網(wǎng)絡(luò)上的博文資料创倔,僅以此文作為學(xué)習(xí)總結(jié)嗡害。BTW,目前Ansible對(duì)python3的支持還不是...
    qiuyi943閱讀 18,209評(píng)論 1 15
  • 黃昏浮出水面 此時(shí)月光正濃畦攘,適合煮酒 陪著自己霸妹,把自己喝醉 把酒醉后的那聲嘆息 寄養(yǎng)在華燈初上 歲月無(wú)情 不容丈量...
    鷹鷹兒閱讀 189評(píng)論 4 1