1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Move print statements out of playbook.py and back into CLI so we can simplify playbook operations

independent of output, and can also see all the output nicely grouped together if we want
to reformat it or make summaries of statistics.
This commit is contained in:
Michael DeHaan 2012-03-06 19:24:36 -05:00
parent de3cff8cef
commit 85e0de5bb2
3 changed files with 41 additions and 14 deletions

View file

@ -21,11 +21,39 @@
import sys
import ansible.playbook
import ansible.constants as C
from ansible.utils import *
import getpass
from optparse import OptionParser
#######################################################
class PlaybookCallbacks(object):
def __init__(self):
pass
def set_playbook(self, playbook):
self.playbook = playbook
def on_start(self):
print "\n"
def on_task_start(self, name, is_conditional):
print task_start_msg(name, is_conditional)
def on_unreachable(self, host, msg):
print "unreachable: [%s] => %s" % (host, msg)
def on_failed(self, host, results):
print "failed: [%s] => %s\n" % (host, smjson(results))
def on_ok(self, host):
print "ok: [%s]\n" % (host)
def on_play_start(self, pattern):
print "PLAY [%s] ****************************\n" % pattern
def main(args):
''' run ansible-playbook operations '''
@ -60,6 +88,7 @@ def main(args):
forks=options.forks,
verbose=True,
remote_pass=sshpass,
callbacks=PlaybookCallbacks()
)
pb.run()

View file

@ -1,5 +1,5 @@
# -*- mode: shell-script -*-
PREFIX_PYTHONPATH="`pwd`/lib/ansible:`pwd`/lib"
PREFIX_PYTHONPATH="`pwd`/lib"
PREFIX_PATH="`pwd`/bin"
PREFIX_MANPATH="`pwd`/docs/man"
PREFIX_ANS_LIBRARY="`pwd`/library"
@ -9,6 +9,7 @@ export PYTHONPATH=$PREFIX_PYTHONPATH:$PYTHONPATH
echo "Prefixing PATH with $PREFIX_PATH"
export PATH=$PREFIX_PATH:$PATH
echo $PATH
echo "Prefixing MANPATH with $PREFIX_MANPATH"
export MANPATH=$PREFIX_MANPATH:$MANPATH

View file

@ -50,7 +50,8 @@ class PlayBook(object):
timeout =C.DEFAULT_TIMEOUT,
remote_user =C.DEFAULT_REMOTE_USER,
remote_pass =C.DEFAULT_REMOTE_PASS,
verbose=False):
verbose=False,
callbacks=None):
# TODO, once ansible-playbook is it's own script this will
# have much LESS parameters to the constructor and will
@ -64,6 +65,8 @@ class PlayBook(object):
self.remote_user = remote_user
self.remote_pass = remote_pass
self.verbose = verbose
self.callbacks = callbacks
self.callbacks.set_playbook(self)
# store the list of changes/invocations/failure counts
# as a dictionary of integers keyed off the hostname
@ -131,10 +134,9 @@ class PlayBook(object):
''' run all patterns in the playbook '''
# loop through all patterns and run them
self.callbacks.on_start()
for pattern in self.playbook:
self._run_pattern(pattern)
if self.verbose:
print "\n"
# summarize the results
results = {}
@ -200,8 +202,7 @@ class PlayBook(object):
# as the result of a change handler on a subset
# of all of the hosts
if self.verbose:
print task_start_msg(name, conditional)
self.callbacks.on_task_start(name, conditional)
# load up an appropriate ansible runner to
# run the task in parallel
@ -222,8 +223,7 @@ class PlayBook(object):
for host, msg in dark.items():
self.processed[host] = 1
if self.verbose:
print "unreachable: [%s] => %s" % (host, msg)
self.callbacks.on_unreachable(host, msg)
if not host in self.dark:
self.dark[host] = 1
else:
@ -233,15 +233,13 @@ class PlayBook(object):
self.processed[host] = 1
if is_failed(results):
if self.verbose:
print "failed: [%s] => %s\n" % (host, smjson(results))
self.callbacks.on_failed(host, results)
if not host in self.failures:
self.failures[host] = 1
else:
self.failures[host] = self.failures[host] + 1
else:
if self.verbose:
print "ok: [%s]\n" % host
self.callbacks.on_ok(host)
if not host in self.invocations:
self.invocations[host] = 1
else:
@ -297,8 +295,7 @@ class PlayBook(object):
self.host_list, groups = ansible.runner.Runner.parse_hosts(self.host_list)
if self.verbose:
print "PLAY [%s] ****************************\n" % pattern
self.callbacks.on_play_start(pattern)
# first run the setup task on every node, which gets the variables
# written to the JSON file and will also bubble facts back up via