mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-05-07 13:52:54 +00:00
Applying callback model to runner, and using that in playbooks, so output can be more immediate in playbooks.
(Runner still does not use callbacks for default output)
This commit is contained in:
@@ -29,6 +29,7 @@ import ansible.runner
|
||||
import ansible.constants as C
|
||||
from ansible import utils
|
||||
from ansible import errors
|
||||
from ansible import callbacks
|
||||
|
||||
########################################################
|
||||
|
||||
@@ -38,7 +39,8 @@ class Cli(object):
|
||||
# ----------------------------------------------
|
||||
|
||||
def __init__(self):
|
||||
pass
|
||||
self.stats = callbacks.AggregateStats()
|
||||
self.callbacks = callbacks.DefaultRunnerCallbacks()
|
||||
|
||||
# ----------------------------------------------
|
||||
|
||||
@@ -98,6 +100,7 @@ class Cli(object):
|
||||
forks=options.forks,
|
||||
background=options.seconds,
|
||||
pattern=pattern,
|
||||
callbacks=self.callbacks,
|
||||
verbose=True,
|
||||
)
|
||||
return (runner, runner.run())
|
||||
@@ -116,6 +119,7 @@ class Cli(object):
|
||||
timeout=old_runner.timeout,
|
||||
forks=old_runner.forks,
|
||||
pattern='*',
|
||||
callbacks=self.callbacks,
|
||||
verbose=True,
|
||||
)
|
||||
|
||||
@@ -178,6 +182,7 @@ class Cli(object):
|
||||
utils.write_tree_file(options.tree, hostname, utils.bigjson(utils.contacted_host_result(results, hostname)))
|
||||
buf += msg
|
||||
|
||||
# TODO: remove, callbacks now do this
|
||||
if utils.has_dark_hosts(results):
|
||||
buf += utils.dark_hosts_msg(results)
|
||||
|
||||
|
||||
@@ -28,14 +28,6 @@ from ansible import errors
|
||||
from ansible import utils
|
||||
from ansible import callbacks
|
||||
|
||||
def summarize(results):
|
||||
''' print out per host statistics '''
|
||||
|
||||
print "PLAY RECAP ******************************\n"
|
||||
hosts = sorted(results.keys())
|
||||
for host in hosts:
|
||||
print "%s : %s" % (host, utils.smjson(results[host]))
|
||||
|
||||
def main(args):
|
||||
''' run ansible-playbook operations '''
|
||||
|
||||
@@ -70,6 +62,11 @@ def main(args):
|
||||
|
||||
# run all playbooks specified on the command line
|
||||
for playbook in args:
|
||||
|
||||
stats = callbacks.AggregateStats()
|
||||
playbook_cb = callbacks.PlaybookCallbacks()
|
||||
runner_cb = callbacks.PlaybookRunnerCallbacks(stats)
|
||||
|
||||
pb = ansible.playbook.PlayBook(
|
||||
playbook=playbook,
|
||||
host_list=options.inventory,
|
||||
@@ -77,13 +74,24 @@ def main(args):
|
||||
forks=options.forks,
|
||||
verbose=True,
|
||||
remote_pass=sshpass,
|
||||
callbacks=callbacks.PlaybookCallbacks(),
|
||||
callbacks=playbook_cb,
|
||||
runner_callbacks=runner_cb,
|
||||
stats=stats,
|
||||
timeout=options.timeout,
|
||||
override_hosts=override_hosts,
|
||||
)
|
||||
try:
|
||||
|
||||
results = pb.run()
|
||||
summarize(results)
|
||||
hosts = sorted(pb.stats.processed.keys())
|
||||
print "\n\nPLAY RECAP **********************\n\n"
|
||||
for h in hosts:
|
||||
t = pb.stats.summarize(h)
|
||||
print "%-30s : ok=%4s changed=%4s unreachable=%4s failed=%4s " % (h,
|
||||
t['ok'], t['changed'], t['unreachable'], t['failures']
|
||||
)
|
||||
print "\n"
|
||||
|
||||
except errors.AnsibleError, e:
|
||||
print >>sys.stderr, "ERROR: %s" % e
|
||||
return 1
|
||||
|
||||
Reference in New Issue
Block a user