Created
August 19, 2025 08:00
-
-
Save l-mb/117ff5c58ac1d0ee75abf0218057f08a to your computer and use it in GitHub Desktop.
ansible callback to track package (requested) (un)installs during playbook run
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
| #!/usr/bin/python3 | |
| from ansible.plugins.callback import CallbackBase | |
| class CallbackModule(CallbackBase): | |
| CALLBACK_VERSION = 2.0 | |
| CALLBACK_TYPE = 'notification' | |
| CALLBACK_NAME = 'package_tracker' | |
| def v2_runner_on_ok(self, result): | |
| if result._task.action in ['package', 'ansible.builtin.package', 'yum', 'apt', 'zypper']: | |
| state = result._task.args.get('state', 'present') | |
| if state in ['present', 'latest', 'installed']: | |
| packages = result._task.args.get('name', []) | |
| if not isinstance(packages, list): | |
| packages = [packages] | |
| with open('/tmp/requested_packages.txt', 'a') as f: | |
| for pkg in packages: | |
| f.write(f"INSTALL: {pkg}\n") | |
| elif state in ['absent', 'removed']: | |
| packages = result._task.args.get('name', []) | |
| if not isinstance(packages, list): | |
| packages = [packages] | |
| with open('/tmp/removed_packages.txt', 'a') as f: | |
| for pkg in packages: | |
| f.write(f"REMOVE: {pkg}\n") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment