Handle dynamically the ansible artifacts dir path

Change-Id: Ic28afceca1decc6f5bf3bdd5a2630c1a44138e5a
This commit is contained in:
Mathieu Bultel 2020-09-23 22:23:37 +02:00 committed by mbu
parent bd122295ef
commit fd226ce290
4 changed files with 48 additions and 4 deletions

View File

@ -366,6 +366,13 @@ class Ansible(object):
if not playbook_dir:
playbook_dir = workdir
if not ansible_artifact_path:
if log_path:
ansible_artifact_path = "{}/artifacts/".format(log_path)
else:
ansible_artifact_path = \
constants.VALIDATION_ANSIBLE_ARTIFACT_PATH
playbook = self._playbook_check(playbook, playbook_dir)
self.log.info(
'Running Ansible playbook: {},'
@ -388,8 +395,6 @@ class Ansible(object):
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()

View File

@ -23,4 +23,5 @@ VALIDATION_GROUPS = ['no-op',
'post']
VALIDATIONS_LOG_BASEDIR = '/var/log/validations/'
VALIDATION_ANSIBLE_ARTIFACT_PATH = '/var/log/validations/artifacts/'
VALIDATION_ANSIBLE_ARTIFACT_PATH = '{}/artifacts/'.format(
VALIDATIONS_LOG_BASEDIR)

View File

@ -151,3 +151,41 @@ class TestAnsible(TestCase):
)
self.assertEquals((_playbook, _rc, _status),
('existing.yaml', None, 'unstarted'))
@mock.patch('six.moves.builtins.open')
@mock.patch('os.path.exists', return_value=True)
@mock.patch('os.makedirs')
@mock.patch.object(Runner, 'run',
return_value=fakes.fake_ansible_runner_run_return(rc=0))
@mock.patch('ansible_runner.utils.dump_artifact', autospec=True,
return_value="/foo/inventory.yaml")
@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_dump_artifact, mock_run, mock_mkdirs,
mock_exists, mock_open):
_playbook, _rc, _status = self.run.run(
playbook='existing.yaml',
inventory='localhost,',
workdir='/tmp',
log_path='/tmp/foo'
)
opt = {
'artifact_dir': '/tmp',
'envvars': {
'ANSIBLE_STDOUT_CALLBACK': 'fake.py',
'ANSIBLE_CONFIG': '/tmp/foo/artifacts/ansible.cfg',
'VALIDATIONS_LOG_DIR': '/tmp/foo'},
'extravars': {},
'fact_cache': '/tmp/foo/artifacts/',
'fact_cache_type': 'jsonfile',
'ident': '',
'inventory': 'localhost,',
'playbook': 'existing.yaml',
'private_data_dir': '/tmp',
'project_dir': '/tmp',
'quiet': False,
'rotate_artifacts': 256,
'verbosity': 0}
mock_config.assert_called_with(**opt)

View File

@ -245,7 +245,7 @@ class ValidationActions(object):
results = []
for playbook in playbooks:
validation_uuid, artifacts_dir = v_utils.create_artifacts_dir(
prefix=os.path.basename(playbook))
dir_path=log_path, prefix=os.path.basename(playbook))
run_ansible = v_ansible(validation_uuid)
_playbook, _rc, _status = run_ansible.run(
workdir=artifacts_dir,