ansible1.9.1版本
写callback插件功能实现
1.ansible.cfg配置文件(变更callback路径位置)
[defaults]
host_key_checking = False
callback_plugins = /tmp/callback
2.编写回显callback脚本
cc.py文件名字可以自定义,但是里面的内容有具体要求
vim /tmp/callback/cc.py
#!/usr/bin/env python
# -*- coding=utf-8 -*-
######################
class CallbackModule(object):
#执行成功的
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']
#执行失败的
def runner_on_failed(self, host, res, ignore_errors=False):
pass
#执行跳过的
def runner_on_skipped(self, host, item=None):
pass
#主机不可达的
def runner_on_unreachable(self, host, res):
pass
是不是很简单,按照标注文件定义CallbackModule类,重写里面的runner_on_ok和runner_on_failed方法就好了,后面接的host,res,以及其他都是固定写法,不能
3.编写yaml文件
/tmp/test.yml
---
- hosts: all
tasks:
- name: test playbook
shell: 'df -h'
/tmp/hosts
[task]
127.0.0.1
4.执行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是固定写法,不能改
尝试print res看下里面的内容,大字典里面各种信息
{
"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"
}
}