From dde275ef26f4397bf84d5168abcb30292c790b7a Mon Sep 17 00:00:00 2001 From: Pierre Riteau Date: Thu, 14 Feb 2019 11:28:16 +0000 Subject: [PATCH] Print output of dump-config in case of errors If Ansible fails to look up variable values, Kayobe exits and only prints this unhelpful message: Kayobe playbook(s) ansible/dump-config.yml exited 2 With this commit, the output is captured and printed in case of errors. For example, when using the hashi_vault lookup module without being authenticated, the output includes: FAILED! => {"msg": "An unhandled exception occurred while running the lookup plugin 'hashi_vault'. Error was a , original message: No Vault Token specified"} Change-Id: Ia5e33a940bc9c4207c5ea6753614908e47884f4b Story: 2004934 Task: 29326 --- kayobe/ansible.py | 8 +++++--- kayobe/tests/unit/test_ansible.py | 25 +++++++++++++------------ 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/kayobe/ansible.py b/kayobe/ansible.py index 296568e87..1851b0d4f 100644 --- a/kayobe/ansible.py +++ b/kayobe/ansible.py @@ -159,7 +159,7 @@ def build_args(parsed_args, playbooks, def run_playbooks(parsed_args, playbooks, extra_vars=None, limit=None, tags=None, quiet=False, - verbose_level=None, check=None): + check_output=False, verbose_level=None, check=None): """Run a Kayobe Ansible playbook.""" _validate_args(parsed_args, playbooks) cmd = build_args(parsed_args, playbooks, @@ -172,10 +172,12 @@ def run_playbooks(parsed_args, playbooks, # playbooks. env.setdefault(CONFIG_PATH_ENV, parsed_args.config_path) try: - utils.run_command(cmd, quiet=quiet, env=env) + utils.run_command(cmd, check_output=check_output, quiet=quiet, env=env) except subprocess.CalledProcessError as e: LOG.error("Kayobe playbook(s) %s exited %d", ", ".join(playbooks), e.returncode) + if check_output: + LOG.error("The output was:\n%s", e.output) sys.exit(e.returncode) @@ -201,7 +203,7 @@ def config_dump(parsed_args, host=None, hosts=None, var_name=None, # results back. playbook_path = utils.get_data_files_path("ansible", "dump-config.yml") run_playbook(parsed_args, playbook_path, - extra_vars=extra_vars, tags=tags, quiet=True, + extra_vars=extra_vars, tags=tags, check_output=True, verbose_level=verbose_level, check=False) hostvars = {} for path in os.listdir(dump_dir): diff --git a/kayobe/tests/unit/test_ansible.py b/kayobe/tests/unit/test_ansible.py index e32d94e4c..6ec7046a5 100644 --- a/kayobe/tests/unit/test_ansible.py +++ b/kayobe/tests/unit/test_ansible.py @@ -51,8 +51,8 @@ class TestCase(unittest.TestCase): "playbook2.yml", ] expected_env = {"KAYOBE_CONFIG_PATH": "/etc/kayobe"} - mock_run.assert_called_once_with(expected_cmd, quiet=False, - env=expected_env) + mock_run.assert_called_once_with(expected_cmd, check_output=False, + quiet=False, env=expected_env) mock_vars.assert_called_once_with("/etc/kayobe") @mock.patch.object(utils, "run_command") @@ -94,8 +94,8 @@ class TestCase(unittest.TestCase): "playbook2.yml", ] expected_env = {"KAYOBE_CONFIG_PATH": "/path/to/config"} - mock_run.assert_called_once_with(expected_cmd, quiet=False, - env=expected_env) + mock_run.assert_called_once_with(expected_cmd, check_output=False, + quiet=False, env=expected_env) mock_vars.assert_called_once_with("/path/to/config") @mock.patch.object(utils, "run_command") @@ -146,7 +146,8 @@ class TestCase(unittest.TestCase): expected_calls = [ mock.call(["which", "kayobe-vault-password-helper"], check_output=True), - mock.call(expected_cmd, quiet=False, env=expected_env) + mock.call(expected_cmd, check_output=False, quiet=False, + env=expected_env) ] self.assertEqual(expected_calls, mock_run.mock_calls) mock_vars.assert_called_once_with("/path/to/config") @@ -174,8 +175,8 @@ class TestCase(unittest.TestCase): "playbook1.yml", ] expected_env = {"KAYOBE_CONFIG_PATH": "/etc/kayobe"} - mock_run.assert_called_once_with(expected_cmd, quiet=False, - env=expected_env) + mock_run.assert_called_once_with(expected_cmd, check_output=False, + quiet=False, env=expected_env) mock_update.assert_called_once_with(mock.ANY, expected_env) @mock.patch.dict(os.environ, {"KAYOBE_VAULT_PASSWORD": "test-pass"}, @@ -203,8 +204,8 @@ class TestCase(unittest.TestCase): ] expected_env = {"KAYOBE_CONFIG_PATH": "/etc/kayobe", "KAYOBE_VAULT_PASSWORD": "test-pass"} - mock_run.assert_called_once_with(expected_cmd, quiet=False, - env=expected_env) + mock_run.assert_called_once_with(expected_cmd, check_output=False, + quiet=False, env=expected_env) @mock.patch.object(utils, "run_command") @mock.patch.object(ansible, "_get_vars_files") @@ -259,8 +260,8 @@ class TestCase(unittest.TestCase): "playbook2.yml", ] expected_env = {"KAYOBE_CONFIG_PATH": "/etc/kayobe"} - mock_run.assert_called_once_with(expected_cmd, quiet=False, - env=expected_env) + mock_run.assert_called_once_with(expected_cmd, check_output=False, + quiet=False, env=expected_env) mock_vars.assert_called_once_with("/etc/kayobe") @mock.patch.object(utils, "run_command") @@ -303,7 +304,7 @@ class TestCase(unittest.TestCase): extra_vars={ "dump_path": dump_dir, }, - quiet=True, tags=None, + check_output=True, tags=None, verbose_level=None, check=False) mock_rmtree.assert_called_once_with(dump_dir) mock_listdir.assert_any_call(dump_dir)