summaryrefslogtreecommitdiffstats
path: root/apex/common/utils.py
diff options
context:
space:
mode:
authorDan Radez <dradez@redhat.com>2017-09-08 12:40:05 -0400
committerDan Radez <dradez@redhat.com>2017-09-11 19:12:57 -0400
commit8669c687a75a00106b055add49b82fee826b8fe8 (patch)
tree747316581fe48c60a7e4201e981c3f79d1b10671 /apex/common/utils.py
parent8d3d5e679fba8e4140730e60809fc4f71cdc098e (diff)
Show ansible tasks as they complete
Change-Id: I1b68d70fd97076a7f2ca68091a6e94d87b72efa2 Signed-off-by: Dan Radez <dradez@redhat.com>
Diffstat (limited to 'apex/common/utils.py')
-rw-r--r--apex/common/utils.py43
1 files changed, 33 insertions, 10 deletions
diff --git a/apex/common/utils.py b/apex/common/utils.py
index e21ab835..b1837b9b 100644
--- a/apex/common/utils.py
+++ b/apex/common/utils.py
@@ -95,13 +95,36 @@ def run_ansible(ansible_vars, playbook, host='localhost', user='root',
with open(ansible_tmp, 'w') as fh:
fh.write("ANSIBLE_HOST_KEY_CHECKING=FALSE {}".format(
' '.join(ansible_command)))
- try:
- my_env = os.environ.copy()
- my_env['ANSIBLE_HOST_KEY_CHECKING'] = 'False'
- logging.info("Executing playbook...this may take some time")
- logging.info(subprocess.check_output(ansible_command, env=my_env,
- stderr=subprocess.STDOUT).decode('utf-8'))
- except subprocess.CalledProcessError as e:
- logging.error("Error executing ansible: {}".format(
- pprint.pformat(e.output.decode('utf-8'))))
- raise
+
+ my_env = os.environ.copy()
+ my_env['ANSIBLE_HOST_KEY_CHECKING'] = 'False'
+ logging.info("Executing playbook...this may take some time")
+ p = subprocess.Popen(ansible_command,
+ stdin=subprocess.PIPE,
+ stdout=subprocess.PIPE,
+ bufsize=1,
+ env=my_env,
+ universal_newlines=True)
+ # read first line
+ x = p.stdout.readline()
+ # initialize task
+ task = ''
+ while x:
+ # append lines to task
+ task += x
+ # log the line and read another
+ x = p.stdout.readline()
+ # deliver the task to info when we get a blank line
+ if not x.strip():
+ task += x
+ logging.info(task.replace('\\n', '\n'))
+ task = ''
+ x = p.stdout.readline()
+ # clean up and get return code
+ p.stdout.close()
+ rc = p.wait()
+ if rc:
+ # raise errors
+ e = "Ansible playbook failed. See Ansible logs for details."
+ logging.error(e)
+ raise Exception(e)