Skip to content

Instantly share code, notes, and snippets.

@sivel
Last active January 26, 2017 18:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sivel/1881b61a57a515ba0ff479d5de8f28bb to your computer and use it in GitHub Desktop.
Save sivel/1881b61a57a515ba0ff479d5de8f28bb to your computer and use it in GitHub Desktop.
Ansible callback plugin to print the number of remaining hosts in a task
changed: [localhost3]
4 hosts remaining: localhost0, localhost1, localhost2, localhost4
changed: [localhost2]
3 hosts remaining: localhost0, localhost1, localhost4
changed: [localhost1]
2 hosts remaining: localhost0, localhost4
changed: [localhost0]
1 hosts remaining: localhost4
changed: [localhost4]
All hosts complete
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
from ansible import constants as C
from ansible.plugins.callback import CallbackBase
try:
from __main__ import cli
except ImportError:
cli = None
class CallbackModule(CallbackBase):
CALLBACK_VERSION = 2.0
CALLBACK_TYPE = 'aggregate'
CALLBACK_NAME = 'remaining_hosts'
def __init__(self, display=None):
self.disabled = False
if cli:
self._options = cli.options
else:
self._options = None
try:
self.forks = self._options.forks
except:
self.forks = C.DEFAULT_FORKS
self.play_hosts = []
self.remaining = []
super(CallbackModule, self).__init__(display=display)
def v2_playbook_on_play_start(self, play):
self.play = play
play_vars = play._variable_manager.get_vars(play._loader,
play=self.play)
self.play_hosts = play_vars['ansible_play_hosts']
self.remaining = self.play_hosts[:]
def v2_runner_on_ok(self, result, **kwargs):
host = result._host.name
self.remaining.remove(host)
if self.remaining:
self._display.display(
'%d hosts remaining: %s' % (len(self.remaining),
', '.join(self.remaining))
)
else:
self._display.display('All hosts complete')
v2_runner_on_failed = v2_runner_on_ok
v2_runner_on_unreachable = v2_runner_on_ok
v2_runner_on_skipped = v2_runner_on_ok
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment