Add handler for exception raising

This change adds a switch that will enable or disable raising an 
exception when a playbook executes. This will allow some methods to 
return the RC and status information when a playbook is run, even when 
there's a failure. The default behaviour is to raise an exception on 
failure, but when fail_on_rc is set to False the run_ansible_playbook
method will return the status and rc information regardless of any 
failures.

To ensure we're not raising an exception from the ansible runner library
its been changed to RuntimeError.

Change-Id: I3af652615b5227144256074c05170d148f19bc1d
Closes-Bug: #1859182
Signed-off-by: Kevin Carter <kecarter@redhat.com>
This commit is contained in:
Kevin Carter 2020-02-20 12:13:34 -06:00 committed by Kevin Carter (cloudnull)
parent 475ace8f60
commit 9902d7469a
4 changed files with 13 additions and 4 deletions

View File

@ -44,7 +44,6 @@ from tripleoclient.tests import fakes
from six.moves.configparser import ConfigParser
from six.moves.urllib import error as url_error
from ansible_runner import AnsibleRunnerException
from ansible_runner import Runner
@ -84,7 +83,7 @@ class TestRunAnsiblePlaybook(TestCase):
def test_subprocess_error(self, mock_dump_artifact,
mock_run, mock_mkdirs, mock_exists,
mock_mkstemp):
with self.assertRaises(AnsibleRunnerException):
with self.assertRaises(RuntimeError):
utils.run_ansible_playbook(
'existing.yaml',
'localhost,',

View File

@ -212,7 +212,8 @@ def run_ansible_playbook(playbook, inventory, workdir, playbook_dir=None,
plan='overcloud', gathering_policy='smart',
extra_env_variables=None, parallel_run=False,
callback_whitelist=None, ansible_cfg=None,
ansible_timeout=30, reproduce_command=False):
ansible_timeout=30, reproduce_command=False,
fail_on_rc=True):
"""Simple wrapper for ansible-playbook.
:param playbook: Playbook filename.
@ -296,6 +297,11 @@ def run_ansible_playbook(playbook, inventory, workdir, playbook_dir=None,
playbook command which is helpful for debugging
and retry purposes.
:type reproduce_command: Boolean
:param fail_on_rc: Enable or disable raising an exception whenever the
return code from the playbook execution results in a
non 0 exit code. The default is True.
:type fail_on_rc: Boolean
"""
def _playbook_check(play):
@ -599,7 +605,9 @@ def run_ansible_playbook(playbook, inventory, workdir, playbook_dir=None,
if not quiet:
LOG.error(err_msg)
raise ansible_runner.AnsibleRunnerException(err_msg)
if fail_on_rc:
raise RuntimeError(err_msg)
LOG.info(
'Ansible execution success. playbook: {}'.format(

View File

@ -1324,6 +1324,7 @@ class Deploy(command.Command):
),
workdir=self.ansible_dir,
extra_env_variables=extra_env_var,
fail_on_rc=False,
**operation
)[0]
except Exception as e:

View File

@ -95,6 +95,7 @@ def _check_diskspace(upgrade=False):
connection='local',
output_callback='validation_output',
playbook_dir=constants.ANSIBLE_VALIDATION_DIR,
fail_on_rc=False,
**playbook_args
)
if rc != 0: