Last active
October 24, 2021 21:38
-
-
Save iNecas/c502b5a9a6e6a801709f to your computer and use it in GitHub Desktop.
Ansible callback plugin to save events into files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
from datetime import datetime | |
import json | |
import uuid | |
TIME_FORMAT="%Y-%m-%d_%H%M%S_%f" | |
FILE_NAME_FORMAT="%(now)s-%(host)s.json" | |
MSG_FORMAT='{"host":"%(host)s","timestamp":"%(now)s", "category":"%(category)s", "data": %(data)s}' + "\n" | |
LOG_DIR="/tmp/ansible/events" | |
AGGREGATION_KEY = uuid.uuid4().hex | |
if not os.path.exists(LOG_DIR): | |
os.makedirs(LOG_DIR) | |
class CallbackModule(object): | |
""" | |
logs playbook results, per host, in LOG_DIR | |
""" | |
def log(self, host, category, data): | |
if type(data) != dict: | |
data = dict(msg=data) | |
data = json.dumps(data) | |
dir_path = os.path.join(LOG_DIR, AGGREGATION_KEY) | |
if not os.path.exists(dir_path): | |
os.makedirs(dir_path) | |
now = datetime.now().strftime(TIME_FORMAT) | |
path = os.path.join(dir_path, FILE_NAME_FORMAT % dict(now=now, host=host)) | |
fd = open(path, "w") | |
fd.write(MSG_FORMAT % dict(host=host, now=now, category=category, data=data)) | |
fd.close() | |
def on_any(self, *args, **kwargs): | |
pass | |
def runner_on_failed(self, host, res, ignore_errors=False): | |
self.log(host, 'FAILED', res) | |
def runner_on_ok(self, host, res): | |
self.log(host, 'OK', res) | |
def runner_on_skipped(self, host, item=None): | |
self.log(host, 'SKIPPED', '...') | |
def runner_on_unreachable(self, host, res): | |
self.log(host, 'UNREACHABLE', res) | |
def runner_on_no_hosts(self): | |
pass | |
def runner_on_async_poll(self, host, res, jid, clock): | |
pass | |
def runner_on_async_ok(self, host, res, jid): | |
pass | |
def runner_on_async_failed(self, host, res, jid): | |
self.log(host, 'ASYNC_FAILED', res) | |
def playbook_on_start(self): | |
pass | |
def playbook_on_notify(self, host, handler): | |
pass | |
def playbook_on_no_hosts_matched(self): | |
pass | |
def playbook_on_no_hosts_remaining(self): | |
pass | |
def playbook_on_task_start(self, name, is_conditional): | |
pass | |
def playbook_on_vars_prompt(self, varname, private=True, prompt=None, encrypt=None, confirm=False, salt_size=None, salt=None, default=None): | |
pass | |
def playbook_on_setup(self): | |
pass | |
def playbook_on_import_for_host(self, host, imported_file): | |
self.log(host, 'IMPORTED', imported_file) | |
def playbook_on_not_import_for_host(self, host, missing_file): | |
self.log(host, 'NOTIMPORTED', missing_file) | |
def playbook_on_play_start(self, name): | |
pass | |
def playbook_on_stats(self, stats): | |
pass |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"host": "test-1.local.txst", | |
"timestamp": "2015-10-26_203706_291688", | |
"category": "OK", | |
"data": { | |
"invocation": { | |
"module_name": "setup", | |
"module_complex_args": { | |
}, | |
"module_args": "" | |
}, | |
"verbose_override": true, | |
"changed": false, | |
"ansible_facts": { | |
"ansible_product_serial": "R9EFB5C", | |
"ansible_form_factor": "Notebook", | |
"ansible_product_version": "ThinkPad T420s", | |
"ansible_fips": false, | |
"ansible_swaptotal_mb": 7951, | |
"ansible_user_id": "root", | |
"module_setup": true, | |
"ansible_userspace_bits": "64", | |
"ansible_distribution_version": "7.1.1503", | |
"ansible_domain": "", | |
"ansible_virtualization_type": "docker", | |
"ansible_processor_cores": 2, | |
"ansible_virtualization_role": "guest", | |
"ansible_env": { | |
"LC_CTYPE": "en_US.UTF-8", | |
"LESSOPEN": "||/usr/bin/lesspipe.sh %s", | |
"SSH_CLIENT": "172.17.42.1 44581 22", | |
"LOGNAME": "root", | |
"USER": "root", | |
"HOME": "/root", | |
"PATH": "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin", | |
"_": "/usr/bin/python", | |
"LANG": "en_US.UTF-8", | |
"TERM": "screen-256color", | |
"SHELL": "/bin/bash", | |
"SHLVL": "2", | |
"SSH_TTY": "/dev/pts/1", | |
"PWD": "/root", | |
"XMODIFIERS": "@im=ibus", | |
"MAIL": "/var/mail/root", | |
"SSH_CONNECTION": "172.17.42.1 44581 172.17.0.2 22" | |
}, | |
"ansible_processor_vcpus": 4, | |
"ansible_bios_version": "8CET51WW (1.31 )", | |
"ansible_processor": [ | |
"GenuineIntel", | |
"Intel(R) Core(TM) i5-2540M CPU @ 2.60GHz", | |
"GenuineIntel", | |
"Intel(R) Core(TM) i5-2540M CPU @ 2.60GHz", | |
"GenuineIntel", | |
"Intel(R) Core(TM) i5-2540M CPU @ 2.60GHz", | |
"GenuineIntel", | |
"Intel(R) Core(TM) i5-2540M CPU @ 2.60GHz" | |
], | |
"ansible_date_time": { | |
"tz": "UTC", | |
"hour": "19", | |
"time": "19:37:06", | |
"tz_offset": "+0000", | |
"month": "10", | |
"epoch": "1445888226", | |
"iso8601_micro": "2015-10-26T19:37:06.277375Z", | |
"weekday": "Monday", | |
"year": "2015", | |
"date": "2015-10-26", | |
"iso8601": "2015-10-26T19:37:06Z", | |
"day": "26", | |
"minute": "37", | |
"second": "06" | |
}, | |
"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": 65536 | |
}, | |
"ansible_memtotal_mb": 7866, | |
"ansible_architecture": "x86_64", | |
"ansible_default_ipv4": { | |
"macaddress": "02:42:ac:11:00:02", | |
"network": "172.17.0.0", | |
"mtu": 1500, | |
"alias": "eth0", | |
"netmask": "255.255.0.0", | |
"address": "172.17.0.2", | |
"interface": "eth0", | |
"type": "ether", | |
"gateway": "172.17.42.1" | |
}, | |
"ansible_swapfree_mb": 7926, | |
"ansible_default_ipv6": { | |
}, | |
"ansible_distribution_release": "Core", | |
"ansible_system_vendor": "LENOVO", | |
"ansible_os_family": "RedHat", | |
"ansible_cmdline": { | |
"LANG": "en_US.UTF-8", | |
"BOOT_IMAGE": "/vmlinuz-4.1.8-100.fc21.x86_64", | |
"rd.dm": "0", | |
"quiet": true, | |
"rd.md": "0", | |
"vconsole.font": "latarcyrheb-sun16", | |
"rhgb": true, | |
"rd.lvm.lv": "fedora/root", | |
"rd.luks.uuid": "luks-a5437b88-b162-4767-bfff-d7a3b0eb13f9", | |
"ro": true, | |
"root": "/dev/mapper/fedora-root", | |
"vconsole.keymap": "us" | |
}, | |
"ansible_user_gid": 0, | |
"ansible_selinux": false, | |
"ansible_userspace_architecture": "x86_64", | |
"ansible_product_uuid": "81206D02-5451-CB11-8699-A4AD905B8798", | |
"ansible_system": "Linux", | |
"ansible_pkg_mgr": "yum", | |
"ansible_memfree_mb": 189, | |
"ansible_devices": { | |
"sda": { | |
"scheduler_mode": "cfq", | |
"rotational": "1", | |
"vendor": "ATA", | |
"sectors": "625142448", | |
"host": "", | |
"sectorsize": "512", | |
"removable": "0", | |
"support_discard": "0", | |
"model": "HITACHI HTS72323", | |
"size": "298.09 GB", | |
"holders": [ | |
], | |
"partitions": { | |
"sda2": { | |
"start": "1026048", | |
"sectorsize": 512, | |
"sectors": "624115712", | |
"size": "297.60 GB" | |
}, | |
"sda1": { | |
"start": "2048", | |
"sectorsize": 512, | |
"sectors": "1024000", | |
"size": "500.00 MB" | |
} | |
} | |
}, | |
"sdb": { | |
"scheduler_mode": "cfq", | |
"rotational": "0", | |
"vendor": "ATA", | |
"sectors": "488397168", | |
"host": "", | |
"sectorsize": "512", | |
"removable": "0", | |
"support_discard": "512", | |
"model": "Samsung SSD 840", | |
"size": "232.89 GB", | |
"holders": [ | |
], | |
"partitions": { | |
"sdb2": { | |
"start": "1026048", | |
"sectorsize": 512, | |
"sectors": "487370752", | |
"size": "232.40 GB" | |
}, | |
"sdb1": { | |
"start": "2048", | |
"sectorsize": 512, | |
"sectors": "1024000", | |
"size": "500.00 MB" | |
} | |
} | |
} | |
}, | |
"ansible_user_uid": 0, | |
"ansible_memory_mb": { | |
"real": { | |
"total": 7866, | |
"free": 189, | |
"used": 7677 | |
}, | |
"swap": { | |
"cached": 4, | |
"total": 7951, | |
"used": 25, | |
"free": 7926 | |
}, | |
"nocache": { | |
"used": 3586, | |
"free": 4280 | |
} | |
}, | |
"ansible_distribution": "CentOS", | |
"ansible_distribution_major_version": "7", | |
"ansible_user_dir": "/root", | |
"ansible_processor_count": 1, | |
"ansible_hostname": "ed5281e9a790", | |
"ansible_bios_date": "11/29/2011", | |
"ansible_all_ipv6_addresses": [ | |
"fe80::42:acff:fe11:2" | |
], | |
"ansible_interfaces": [ | |
"lo", | |
"eth0" | |
], | |
"ansible_machine": "x86_64", | |
"ansible_ssh_host_key_rsa_public": "AAAAB3NzaC1yc2EAAAADAQABAAACAQDLk7pSSAjclV75WfEan/Vfg1/MWzgNZ2uUSLxgVPaMJMiyW70yBAQAjiGNy3jcGwg6ozjOK3j8qL7ZTIP8+9IaqFFxYNHnAAlSoPKGYoi2cSqt8w4Bq1z8I0IfRG2sAAP4rlNWdsyUfUElz6CbWCxStmePtUXHJhko8S88h/bgIwpOqGdSXw4DJFKoU+WqVL61kqYGAyg+OOGsGE4GpVrapBUDr5txEOHjeClkkn/TY98BViDPD1Lm0MTvTnjBMyTGg1xE4aJsS0uSDcXubopkWd2Pt+LS4unp25rZxa7MMNaqsYUoEpxugTnjL7jsr/o0zmBciEU0fJ6PMg4pKsGnxKvHH/vIv8kRATfnGBT0Y3Q87P7gYv1XQANMQdYH2faPIrj42/B2dYhfhVFGR37ht1wkffy01F9hbEMtizEkaqKCr9gg54p8s4P1tdOVHWkKgOP/nv96d1JwaXttNSI7DGUs1pK7Ium3L3NOXsMU7nkD+tsXmVOZ7iFslH0PIHzcgPsPVzNv6ulLnOhoaao6TcRQToq3OSPSHCzpY9ZfnD1cwnWLYk24uoiJt+lrZ3C+0XvgQ0tqkgrhIayFo+SSVgd89zOwlnGzhLiI/Vqx8VRIZXDjA7qS+dtM56Wpiy182aRLiZbLEDoqAJvc8AgpBQm4Q==", | |
"ansible_user_gecos": "root", | |
"ansible_kernel": "4.1.8-100.fc21.x86_64", | |
"ansible_processor_threads_per_core": 2, | |
"ansible_fqdn": "ed5281e9a790", | |
"ansible_mounts": [ | |
{ | |
"options": "rw,context=\"system_u:object_r:svirt_sandbox_file_t:s0:c262,c727\",relatime,stripe=16,data=ordered", | |
"uuid": "NA", | |
"size_total": 10434699264, | |
"device": "/dev/mapper/docker-253:1-2099011-ed5281e9a790b08e09608a312211667a9b9e9ab568c42b557ee9be2986ab2af7", | |
"mount": "/", | |
"size_available": 9526153216, | |
"fstype": "ext4" | |
}, | |
{ | |
"options": "rw,seclabel,relatime,data=ordered", | |
"uuid": "NA", | |
"size_total": 184426045440, | |
"device": "/dev/mapper/fedora-home", | |
"mount": "/opt/src", | |
"size_available": 20729679872, | |
"fstype": "ext4" | |
}, | |
{ | |
"options": "rw,seclabel,relatime,data=ordered", | |
"uuid": "NA", | |
"size_total": 52710469632, | |
"device": "/dev/mapper/fedora-root", | |
"mount": "/etc/resolv.conf", | |
"size_available": 6322130944, | |
"fstype": "ext4" | |
}, | |
{ | |
"options": "rw,seclabel,relatime,data=ordered", | |
"uuid": "NA", | |
"size_total": 52710469632, | |
"device": "/dev/mapper/fedora-root", | |
"mount": "/etc/hostname", | |
"size_available": 6322130944, | |
"fstype": "ext4" | |
}, | |
{ | |
"options": "rw,seclabel,relatime,data=ordered", | |
"uuid": "NA", | |
"size_total": 52710469632, | |
"device": "/dev/mapper/fedora-root", | |
"mount": "/etc/hosts", | |
"size_available": 6322130944, | |
"fstype": "ext4" | |
} | |
], | |
"ansible_eth0": { | |
"macaddress": "02:42:ac:11:00:02", | |
"mtu": 1500, | |
"active": true, | |
"promisc": false, | |
"ipv4": { | |
"netmask": "255.255.0.0", | |
"network": "172.17.0.0", | |
"address": "172.17.0.2" | |
}, | |
"ipv6": [ | |
{ | |
"scope": "link", | |
"prefix": "64", | |
"address": "fe80::42:acff:fe11:2" | |
} | |
], | |
"device": "eth0", | |
"type": "ether" | |
}, | |
"ansible_python_version": "2.7.5", | |
"ansible_product_name": "4174BH4", | |
"ansible_user_shell": "/bin/bash", | |
"ansible_all_ipv4_addresses": [ | |
"172.17.0.2" | |
], | |
"ansible_nodename": "ed5281e9a790" | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"host": "test-1.local.txst", | |
"timestamp": "2015-10-26_203713_819495", | |
"category": "OK", | |
"data": { | |
"msg": "", | |
"invocation": { | |
"module_name": "yum", | |
"module_complex_args": { | |
}, | |
"module_args": "name=zsh state=latest" | |
}, | |
"changed": true, | |
"results": [ | |
"Loaded plugins: fastestmirror\nLoading mirror speeds from cached hostfile\n * base: mirror.hosting90.cz\n * extras: mirror.hosting90.cz\n * updates: ftp.cvut.cz\nResolving Dependencies\n--> Running transaction check\n---> Package zsh.x86_64 0:5.0.2-7.el7_1.1 will be installed\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package Arch Version Repository Size\n================================================================================\nInstalling:\n zsh x86_64 5.0.2-7.el7_1.1 updates 2.4 M\n\nTransaction Summary\n================================================================================\nInstall 1 Package\n\nTotal download size: 2.4 M\nInstalled size: 5.6 M\nDownloading packages:\nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n Installing : zsh-5.0.2-7.el7_1.1.x86_64 1/1 \n Verifying : zsh-5.0.2-7.el7_1.1.x86_64 1/1 \n\nInstalled:\n zsh.x86_64 0:5.0.2-7.el7_1.1 \n\nComplete!\n" | |
], | |
"rc": 0 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment