Use consistent working dir for ansible-runner

Instead of using tmp dirs for ansible-runner, use the consistent working
directory, which allows for saving the output for debugging and
reproducability.

Change-Id: I83ad02817ace364eb4bc596127cfd7d6699c32aa
Signed-off-by: James Slagle <jslagle@redhat.com>
This commit is contained in:
James Slagle 2021-03-24 15:09:53 -04:00
parent 252c15bf19
commit 6568a7953b
6 changed files with 65 additions and 37 deletions

View File

@ -216,6 +216,7 @@ class FakeClientWrapper(object):
class FakeRunnerConfig(object):
env = dict() # noqa
artifact_dir = ''
def prepare(self):
pass

View File

@ -63,6 +63,8 @@ class TestRunAnsiblePlaybook(TestCase):
'overcloud'
)
)
ansible_runner.Runner.stdout = mock.MagicMock()
ansible_runner.Runner.stdout.read = mock.MagicMock(return_value='')
def tearDown(self):
utils.constants.DEFAULT_WORK_DIR = self.orig_workdir

View File

@ -244,6 +244,7 @@ class TestDeleteNode(fakes.TestDeleteNode):
playbook_dir='/usr/share/ansible/tripleo-playbooks',
verbosity=mock.ANY,
extra_vars=mock.ANY,
reproduce_command=True,
),
mock.call(
playbook=mock.ANY,

View File

@ -373,7 +373,7 @@ def run_ansible_playbook(playbook, inventory, workdir, playbook_dir=None,
)
return ansible_runner.utils.dump_artifact(
inventory,
ansible_artifact_path,
workdir,
constants.ANSIBLE_HOSTS_FILENAME
)
@ -708,6 +708,14 @@ def run_ansible_playbook(playbook, inventory, workdir, playbook_dir=None,
_log_path = r_opts['envvars']['ANSIBLE_LOG_PATH']
if os.path.isfile(_log_path):
os.chown(_log_path, get_uid, -1)
# Save files we care about
with open(os.path.join(workdir, 'stdout'), 'w') as f:
f.write(runner.stdout.read())
for output in 'status', 'rc':
val = getattr(runner, output)
if val:
with open(os.path.join(workdir, output), 'w') as f:
f.write(str(val))
if rc != 0:
err_msg = (

View File

@ -1066,7 +1066,8 @@ class DeployOvercloud(command.Command):
overcloud_endpoint = utils.get_overcloud_endpoint(stack)
horizon_url = deployment.get_horizon_url(
stack=stack.stack_name,
heat_type=parsed_args.heat_type)
heat_type=parsed_args.heat_type,
working_dir=self.working_dir)
rc_params = utils.get_rc_params(
self.orchestration_client,
parsed_args.stack)

View File

@ -255,7 +255,8 @@ def config_download(log, clients, stack, ssh_network='ctlplane',
ansible_playbook_name='deploy_steps_playbook.yaml',
limit_hosts=None, extra_vars=None, inventory_path=None,
ssh_user='tripleo-admin', tags=None, skip_tags=None,
deployment_timeout=None, forks=None, setup_only=False):
deployment_timeout=None, forks=None, setup_only=False,
working_dir=None):
"""Run config download.
:param log: Logging object
@ -317,6 +318,10 @@ def config_download(log, clients, stack, ssh_network='ctlplane',
:param setup_only: Only generates the config-download directory
without executing the actual playbooks.
:type setup_only: Boolean
:param working_dir: Consistent working directory used for generated
ansible files.
:type working_dir: String
"""
def _log_and_print(message, logger, level='info', print_msg=True):
@ -344,6 +349,9 @@ def config_download(log, clients, stack, ssh_network='ctlplane',
if not output_dir:
output_dir = DEFAULT_WORK_DIR
if not working_dir:
working_dir = utils.get_default_working_dir(stack.stack_name)
if not deployment_options:
deployment_options = dict()
@ -386,23 +394,26 @@ def config_download(log, clients, stack, ssh_network='ctlplane',
key_file = utils.get_key(stack.stack_name)
python_interpreter = deployment_options.get('ansible_python_interpreter')
with utils.TempDirs() as tmp:
utils.run_ansible_playbook(
playbook='cli-config-download.yaml',
inventory='localhost,',
workdir=tmp,
playbook_dir=ANSIBLE_TRIPLEO_PLAYBOOKS,
verbosity=verbosity,
extra_vars={
'plan': stack.stack_name,
'output_dir': output_dir,
'ansible_ssh_user': ssh_user,
'ansible_ssh_private_key_file': key_file,
'ssh_network': ssh_network,
'python_interpreter': python_interpreter,
'inventory_path': inventory_path
}
)
playbook = 'cli-config-download.yaml'
ansible_work_dir = os.path.join(
working_dir, os.path.splitext(playbook)[0])
utils.run_ansible_playbook(
playbook='cli-config-download.yaml',
inventory='localhost,',
workdir=ansible_work_dir,
playbook_dir=ANSIBLE_TRIPLEO_PLAYBOOKS,
verbosity=verbosity,
reproduce_command=True,
extra_vars={
'plan': stack.stack_name,
'output_dir': output_dir,
'ansible_ssh_user': ssh_user,
'ansible_ssh_private_key_file': key_file,
'ssh_network': ssh_network,
'python_interpreter': python_interpreter,
'inventory_path': inventory_path
}
)
# If we only want to generate config-download directory, we can quit here.
if setup_only:
@ -484,7 +495,8 @@ def config_download(log, clients, stack, ssh_network='ctlplane',
def get_horizon_url(stack, verbosity=0,
heat_type='installed'):
heat_type='installed',
working_dir=None):
"""Return horizon URL string.
:params stack: Stack name
@ -495,26 +507,29 @@ def get_horizon_url(stack, verbosity=0,
try:
if heat_type != 'installed' and tc_heat_utils.heatclient:
tc_heat_utils.heatclient.save_environment()
with utils.TempDirs() as tmp:
horizon_tmp_file = os.path.join(tmp, 'horizon_url')
utils.run_ansible_playbook(
playbook='cli-undercloud-get-horizon-url.yaml',
inventory='localhost,',
workdir=tmp,
playbook_dir=ANSIBLE_TRIPLEO_PLAYBOOKS,
verbosity=verbosity,
extra_vars={
'stack_name': stack,
'horizon_url_output_file': horizon_tmp_file
}
)
with open(horizon_tmp_file) as f:
return f.read().strip()
playbook = 'cli-undercloud-get-horizon-url.yaml'
ansible_work_dir = os.path.join(
working_dir, os.path.splitext(playbook)[0])
horizon_file = os.path.join(ansible_work_dir, 'horizon_url')
utils.run_ansible_playbook(
playbook=playbook,
inventory='localhost,',
workdir=ansible_work_dir,
playbook_dir=ANSIBLE_TRIPLEO_PLAYBOOKS,
verbosity=verbosity,
reproduce_command=True,
extra_vars={
'stack_name': stack,
'horizon_url_output_file': horizon_file
}
)
finally:
if heat_type != 'installed' and tc_heat_utils.heatclient:
tc_heat_utils.heatclient.restore_environment()
with open(horizon_file) as f:
return f.read().strip()
def get_deployment_status(clients, stack_name, working_dir):
"""Return current deployment status."""