Allow to provide different stdout callback to the libs
This patch depends on the callback modifcation which allow to give other custom or default/standard callback for the stdout from the user. With the current situation, when passing another callback than validation_json it breaks the VF logging. We need to separate logging and stdout. Change-Id: I1566d9763212f9538fea1c0885208db6b949023f
This commit is contained in:
parent
5d43279cc4
commit
a0db6e4892
@ -111,22 +111,26 @@ class Ansible(object):
|
||||
extravars.update(yaml.safe_load(f.read()))
|
||||
return extravars
|
||||
|
||||
def _callback_whitelist(self, callback_whitelist, output_callback):
|
||||
"""Set callback whitelist"""
|
||||
if callback_whitelist:
|
||||
callback_whitelist = ','.join([callback_whitelist,
|
||||
output_callback])
|
||||
else:
|
||||
callback_whitelist = output_callback
|
||||
return ','.join([callback_whitelist, 'profile_tasks'])
|
||||
def _callbacks(self, callback_whitelist, output_callback, envvars={},
|
||||
env={}):
|
||||
"""Set callbacks"""
|
||||
# if output_callback is exported in env, then use it
|
||||
if isinstance(envvars, dict):
|
||||
env.update(envvars)
|
||||
output_callback = env.get('ANSIBLE_STDOUT_CALLBACK', output_callback)
|
||||
callback_whitelist = ','.join(filter(None, [callback_whitelist,
|
||||
output_callback,
|
||||
'validation_json',
|
||||
'profile_tasks']))
|
||||
return callback_whitelist, output_callback
|
||||
|
||||
def _ansible_env_var(self, output_callback, ssh_user, workdir, connection,
|
||||
gathering_policy, module_path, key,
|
||||
extra_env_variables, ansible_timeout,
|
||||
callback_whitelist, base_dir, python_interpreter):
|
||||
callback_whitelist, base_dir, python_interpreter,
|
||||
env={}):
|
||||
"""Handle Ansible env var for Ansible config execution"""
|
||||
cwd = os.getcwd()
|
||||
env = os.environ.copy()
|
||||
env['ANSIBLE_SSH_ARGS'] = (
|
||||
'-o UserKnownHostsFile={} '
|
||||
'-o StrictHostKeyChecking=no '
|
||||
@ -274,7 +278,7 @@ class Ansible(object):
|
||||
return env
|
||||
|
||||
def run(self, playbook, inventory, workdir, playbook_dir=None,
|
||||
connection='smart', output_callback='yaml',
|
||||
connection='smart', output_callback=None,
|
||||
base_dir=constants.DEFAULT_VALIDATIONS_BASEDIR,
|
||||
ssh_user='root', key=None, module_path=None,
|
||||
limit_hosts=None, tags=None, skip_tags=None,
|
||||
@ -384,17 +388,24 @@ class Ansible(object):
|
||||
)
|
||||
)
|
||||
|
||||
# ansible_fact_path = self._creates_ansible_fact_dir()
|
||||
# Get env variables:
|
||||
env = {}
|
||||
env = os.environ.copy()
|
||||
extravars = self._get_extra_vars(extra_vars)
|
||||
callback_whitelist = self._callback_whitelist(callback_whitelist,
|
||||
output_callback)
|
||||
|
||||
callback_whitelist, output_callback = self._callbacks(
|
||||
callback_whitelist,
|
||||
output_callback,
|
||||
extra_env_variables,
|
||||
env)
|
||||
# Set ansible environment variables
|
||||
env = self._ansible_env_var(output_callback, ssh_user, workdir,
|
||||
connection, gathering_policy, module_path,
|
||||
key, extra_env_variables, ansible_timeout,
|
||||
callback_whitelist, base_dir,
|
||||
python_interpreter)
|
||||
env.update(self._ansible_env_var(output_callback, ssh_user, workdir,
|
||||
connection, gathering_policy,
|
||||
module_path, key, extra_env_variables,
|
||||
ansible_timeout, callback_whitelist,
|
||||
base_dir, python_interpreter))
|
||||
|
||||
if not ansible_artifact_path:
|
||||
ansible_artifact_path = constants.VALIDATION_ANSIBLE_ARTIFACT_PATH
|
||||
if 'ANSIBLE_CONFIG' not in env and not ansible_cfg:
|
||||
ansible_cfg = os.path.join(ansible_artifact_path, 'ansible.cfg')
|
||||
config = configparser.ConfigParser()
|
||||
|
@ -169,7 +169,8 @@ class TestAnsible(TestCase):
|
||||
@mock.patch('ansible_runner.runner_config.RunnerConfig')
|
||||
@mock.patch('validations_libs.ansible.Ansible._ansible_env_var',
|
||||
return_value={'ANSIBLE_STDOUT_CALLBACK': 'fake.py'})
|
||||
def test_run_specific_log_path(self, mock_env_var, mock_config,
|
||||
@mock.patch('os.environ.copy', return_value={})
|
||||
def test_run_specific_log_path(self, mock_env, mock_env_var, mock_config,
|
||||
mock_dump_artifact, mock_run, mock_mkdirs,
|
||||
mock_exists, mock_open):
|
||||
_playbook, _rc, _status = self.run.run(
|
||||
|
@ -134,7 +134,8 @@ class ValidationActions(object):
|
||||
extra_env_vars=None, ansible_cfg=None, quiet=True,
|
||||
workdir=None, limit_hosts=None, run_async=False,
|
||||
base_dir=constants.DEFAULT_VALIDATIONS_BASEDIR,
|
||||
log_path=None, python_interpreter=None):
|
||||
log_path=None, python_interpreter=None,
|
||||
output_callback='validation_stdout'):
|
||||
"""Run one or multiple validations by name(s) or by group(s)
|
||||
|
||||
:param validation_name: A list of validation names
|
||||
@ -177,6 +178,9 @@ class ValidationActions(object):
|
||||
``auto_silent`` or the default one
|
||||
``auto_legacy``)
|
||||
:type python_interpreter: ``string``
|
||||
:param output_callback: The Callback plugin to use.
|
||||
(Defaults to 'validation_stdout')
|
||||
:type output_callback: ``string``
|
||||
|
||||
:return: A list of dictionary containing the informations of the
|
||||
validations executions (Validations, Duration, Host_Group,
|
||||
@ -254,7 +258,7 @@ class ValidationActions(object):
|
||||
playbook_dir=validations_dir,
|
||||
parallel_run=True,
|
||||
inventory=inventory,
|
||||
output_callback='validation_json',
|
||||
output_callback=output_callback,
|
||||
quiet=quiet,
|
||||
extra_vars=extra_vars,
|
||||
limit_hosts=limit_hosts,
|
||||
|
Loading…
x
Reference in New Issue
Block a user