Fixes #3973 live output for ansible-pull

Add a -v/--verbose option to ansible-pull to allow passing -vvvv to
ansible-playbook and to show stdout while the playbook executes
This commit is contained in:
James Tanner 2013-12-19 15:38:15 -05:00
parent afa8f36f6b
commit 3b2a35ae00

View file

@ -45,6 +45,8 @@ import sys
import datetime
import socket
from ansible import utils
import shlex
import select
DEFAULT_REPO_TYPE = 'git'
DEFAULT_PLAYBOOK = 'local.yml'
@ -52,6 +54,38 @@ PLAYBOOK_ERRORS = {1: 'File does not exist',
2: 'File is not readable'}
def _run_verbose(cmd):
cmdargs = shlex.split(cmd)
p = subprocess.Popen(cmdargs, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout = ''
stderr = ''
rpipes = [p.stdout, p.stderr]
while True:
rfd, wfd, efd = select.select(rpipes, [], rpipes, 1)
if p.stdout in rfd:
dat = os.read(p.stdout.fileno(), 100)
sys.stdout.write(dat)
stdout += dat
if dat == '':
rpipes.remove(p.stdout)
if p.stderr in rfd:
dat = os.read(p.stderr.fileno(), 100)
stderr += dat
sys.stdout.write(dat)
if dat == '':
rpipes.remove(p.stderr)
# only break out if we've emptied the pipes, or there is nothing to
# read from and the process has finished.
if (not rpipes or not rfd) and p.poll() is not None:
break
# Calling wait while there are still pipes to read can cause a lock
elif not rpipes and p.poll() == None:
p.wait()
return p.returncode, stdout
def _run(cmd):
print >>sys.stderr, "Running: '%s'" % cmd
cmd = subprocess.Popen(cmd, shell=True,
@ -102,6 +136,8 @@ def main(args):
""" Set up and run a local playbook """
usage = "%prog [options] [playbook.yml]"
parser = utils.SortedOptParser(usage=usage)
parser.add_option('-v', '--verbose', default=False, action='store_true',
help='Print the ansible-playbook output while running')
parser.add_option('--purge', default=False, action='store_true',
help='purge checkout after playbook run')
parser.add_option('-o', '--only-if-changed', dest='ifchanged', default=False, action='store_true',
@ -141,7 +177,10 @@ def main(args):
inv_opts = 'localhost,'
limit_opts = 'localhost:%s:127.0.0.1' % hostname
base_opts = '-c local --limit "%s"' % limit_opts
if not options.verbose:
base_opts = '-c local --limit "%s"' % limit_opts
else:
base_opts = '-vvvv -c local --limit "%s"' % limit_opts
repo_opts = "name=%s dest=%s" % (options.url, options.dest)
if options.checkout:
repo_opts += ' version=%s' % options.checkout
@ -172,7 +211,10 @@ def main(args):
if options.inventory:
cmd += ' -i "%s"' % options.inventory
os.chdir(options.dest)
rc, out = _run(cmd)
if not options.verbose:
rc, out = _run(cmd)
else:
rc, out = _run_verbose(cmd)
if options.purge:
os.chdir('/')