jpmens
(Jan-Piet Mens)
December 5, 2024, 6:43pm
1
I’m looking for interesting/cute/exciting/novel ways of using Ansible’s set_stats module.
Other than you can use it to count something or other and display it at playbook end , there’s not much that occurs to me.
What are you using it for?
1 Like
sivel
(sivel)
December 5, 2024, 7:08pm
2
The main reason for introducing it was for tower/awx so that variables could be added in a location that could be used in a workflow for subsequent ansible-playbook
executions. The output from the first execution, is injected into the 2nd using --extra-vars
.
I have an example of how this works outside of awx:
00-README.md
# dump_stats Ansible callback plugin
This callback plugin can aid in replicating the `set_stats` workflow behavior in Tower
It allows you to dump the stats set with `set_stats` to a file, and then use that file with `--extra-vars` in subsequent `ansible-playbook` calls.
## Usage
1. Download `dump_stats.py` file to a `callback_plugins` directory relative to your playbook
1. Run `ansible-playbook` with `ANSIBLE_CALLBACK_WHITELIST=dump_stats`
This file has been truncated. show original
dump_stats.py
# (c) 2020 Matt Martz <matt@sivel.net>
# GNU General Public License v3.0+
# (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
# Make coding more python3-ish
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
DOCUMENTATION = '''
callback: dump_stats
This file has been truncated. show original
# (c) 2020 Matt Martz <matt@sivel.net>
# GNU General Public License v3.0+
# (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
# Make coding more python3-ish
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
DOCUMENTATION = '''
name: dump_stats
short_description: Callback to dump to stats from set_stat to a JSON file
description:
- Dumps the stats from set_stat to a JSON file that can be fed to subsequent
ansible-playbook calls using --extra-vars
type: aggregate
options:
output:
description: Output path to JSON file, defaults to stats.json in the CWD
default: stats.json
env:
This file has been truncated. show original
3 Likes
jpmens
(Jan-Piet Mens)
December 6, 2024, 4:52pm
3
Very interesting, thank you, Matt.