Overload stdout callback plugin

The stdout callback plugin is being set within ansible runner making it
not respect the user defined configuration. This change adds the required
environment variable after the prepare function to ensure our defined
output callback plugin is always used.

Because we're allowing the assumed output callback plugin to be user defined
we need to cater for parallel playbook execution by defining
`directory_isolation_base_path` in config. Now, whenever parallel playbook
execution is required, `directory_isolation_base_path` will be used ensuring
there are no collisions at runtime.

Change-Id: I7c7e7472d238bd2ab3316cac3dbee01f6d0b10e9
Signed-off-by: Kevin Carter <kecarter@redhat.com>
This commit is contained in:
Kevin Carter 2019-12-09 08:00:40 -06:00 committed by Kevin Carter (cloudnull)
parent 87c4c66f39
commit 49d3f725f2
4 changed files with 40 additions and 6 deletions

View File

@ -82,6 +82,13 @@ class FakeClientWrapper(object):
return self.ws
class FakeRunnerConfig(object):
env = dict() # noqa
def prepare(self):
pass
def fake_ansible_runner_run_return(rc=0):
return 'Test Status', rc

View File

@ -860,9 +860,9 @@ class TestDeployUndercloud(TestPluginV1):
)
@mock.patch.object(
ansible_runner.runner_config.RunnerConfig,
'prepare',
return_value=fakes.fake_ansible_runner_run_return()
ansible_runner.runner_config,
'RunnerConfig',
return_value=fakes.FakeRunnerConfig()
)
@mock.patch.object(
ansible_runner.Runner,

View File

@ -124,7 +124,7 @@ class TempDirs(object):
:type dir_path: `string`
:param dir_prefix: prefix to add to a temp directory
:type dir_prefix: `string`
:paramm cleanup: when enabled the temp directory will be
:param cleanup: when enabled the temp directory will be
removed on exit.
:type cleanup: `boolean`
:param chdir: Change to/from the created temporary dir on enter/exit.
@ -207,7 +207,8 @@ def run_ansible_playbook(playbook, inventory, workdir, playbook_dir=None,
limit_hosts=None, tags=None, skip_tags=None,
verbosity=0, quiet=False, extra_vars=None,
plan='overcloud', gathering_policy='smart',
extra_env_variables=None):
extra_env_variables=None, parallel_run=False,
callback_whitelist=None):
"""Simple wrapper for ansible-playbook.
:param playbook: Playbook filename.
@ -229,6 +230,12 @@ def run_ansible_playbook(playbook, inventory, workdir, playbook_dir=None,
:param output_callback: Callback for output format. Defaults to "json".
:type output_callback: String
:param callback_whitelist: Comma separated list of callback plugins.
Defaults to the value of `output_callback`.
When the verbosity is > 0 "profile_tasks"
will also be whitelisted.
:type callback_whitelist: String
:param ssh_user: User for the ssh connection.
:type ssh_user: String
@ -336,6 +343,14 @@ def run_ansible_playbook(playbook, inventory, workdir, playbook_dir=None,
with open(extra_vars) as f:
extravars.update(yaml.safe_load(f.read()))
if callback_whitelist:
callback_whitelist = ','.join([callback_whitelist, output_callback])
else:
callback_whitelist = output_callback
if verbosity > 0:
callback_whitelist = ','.join([callback_whitelist, 'profile_tasks'])
env = os.environ.copy()
env['ANSIBLE_DISPLAY_FAILED_STDERR'] = True
env['ANSIBLE_FORKS'] = 36
@ -418,7 +433,7 @@ def run_ansible_playbook(playbook, inventory, workdir, playbook_dir=None,
constants.DEFAULT_VALIDATIONS_BASEDIR
)
)
env['ANSIBLE_CALLBACK_WHITELIST'] = 'profile_tasks,validation_output'
env['ANSIBLE_CALLBACK_WHITELIST'] = callback_whitelist
env['ANSIBLE_RETRY_FILES_ENABLED'] = False
env['ANSIBLE_HOST_KEY_CHECKING'] = False
env['ANSIBLE_TRANSPORT'] = connection
@ -482,8 +497,19 @@ def run_ansible_playbook(playbook, inventory, workdir, playbook_dir=None,
if limit_hosts:
r_opts['limit'] = limit_hosts
if parallel_run:
r_opts['directory_isolation_base_path'] = ansible_artifact_path
runner_config = ansible_runner.runner_config.RunnerConfig(**r_opts)
runner_config.prepare()
# NOTE(cloudnull): overload the output callback after prepare
# to define the specific format we want.
# This is only required until PR
# https://github.com/ansible/ansible-runner/pull/387
# is merged and released. After this PR has been
# made available to us, this line should be removed.
runner_config.env['ANSIBLE_STDOUT_CALLBACK'] = \
r_opts['envvars']['ANSIBLE_STDOUT_CALLBACK']
runner = ansible_runner.Runner(config=runner_config)
status, rc = runner.run()

View File

@ -407,6 +407,7 @@ class TripleOValidatorRun(command.Command):
workdir=tmp,
playbook=playbook,
playbook_dir=constants.ANSIBLE_VALIDATION_DIR,
parallel_run=True,
inventory=static_inventory,
output_callback='validation_output',
quiet=True,