diff --git a/kayobe/ansible.py b/kayobe/ansible.py index 56d1e57f0f001832d4194665cba05c38424294d6..c7b930bb8987ad198e87dcf89245cbd804315479 100644 --- a/kayobe/ansible.py +++ b/kayobe/ansible.py @@ -118,12 +118,12 @@ def _get_vars_files(config_path): def build_args(parsed_args, playbooks, extra_vars=None, limit=None, tags=None, verbose_level=None, - check=None, ignore_limit=False): + check=None, ignore_limit=False, list_tasks=None): """Build arguments required for running Ansible playbooks.""" cmd = ["ansible-playbook"] if verbose_level: cmd += ["-" + "v" * verbose_level] - if parsed_args.list_tasks: + if list_tasks or (parsed_args.list_tasks and list_tasks is None): cmd += ["--list-tasks"] cmd += vault.build_args(parsed_args, "--vault-password-file") inventory = _get_inventory_path(parsed_args) @@ -160,13 +160,13 @@ def build_args(parsed_args, playbooks, def run_playbooks(parsed_args, playbooks, extra_vars=None, limit=None, tags=None, quiet=False, check_output=False, verbose_level=None, check=None, - ignore_limit=False): + ignore_limit=False, list_tasks=None): """Run a Kayobe Ansible playbook.""" _validate_args(parsed_args, playbooks) cmd = build_args(parsed_args, playbooks, extra_vars=extra_vars, limit=limit, tags=tags, verbose_level=verbose_level, check=check, - ignore_limit=ignore_limit) + ignore_limit=ignore_limit, list_tasks=list_tasks) env = os.environ.copy() vault.update_environment(parsed_args, env) # If the configuration path has been specified via --config-path, ensure @@ -201,12 +201,13 @@ def config_dump(parsed_args, host=None, hosts=None, var_name=None, extra_vars["dump_var_name"] = var_name if facts is not None: extra_vars["dump_facts"] = facts - # Don't use check mode for configuration dumps as we won't get any - # results back. + # Don't use check mode or list tasks for configuration dumps as we + # won't get any 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, check_output=True, - verbose_level=verbose_level, check=False) + verbose_level=verbose_level, check=False, + list_tasks=False) hostvars = {} for path in os.listdir(dump_dir): LOG.debug("Found dump file %s", path) diff --git a/kayobe/tests/unit/test_ansible.py b/kayobe/tests/unit/test_ansible.py index 72ce3c549f67158c7e9af536fa2f731b72fe5bf3..b1df9f5fb18296968218759b1d41bd64416f34f2 100644 --- a/kayobe/tests/unit/test_ansible.py +++ b/kayobe/tests/unit/test_ansible.py @@ -293,6 +293,35 @@ class TestCase(unittest.TestCase): quiet=False, env=expected_env) mock_vars.assert_called_once_with("/etc/kayobe") + @mock.patch.object(utils, "run_command") + @mock.patch.object(ansible, "_get_vars_files") + @mock.patch.object(ansible, "_validate_args") + def test_run_playbooks_list_tasks_arg(self, mock_validate, mock_vars, + mock_run): + mock_vars.return_value = [] + parser = argparse.ArgumentParser() + ansible.add_args(parser) + vault.add_args(parser) + args = [ + "--list-tasks", + ] + parsed_args = parser.parse_args(args) + kwargs = { + "list_tasks": False + } + ansible.run_playbooks(parsed_args, ["playbook1.yml", "playbook2.yml"], + **kwargs) + expected_cmd = [ + "ansible-playbook", + "--inventory", "/etc/kayobe/inventory", + "playbook1.yml", + "playbook2.yml", + ] + expected_env = {"KAYOBE_CONFIG_PATH": "/etc/kayobe"} + 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") @mock.patch.object(ansible, "_get_vars_files") @mock.patch.object(ansible, "_validate_args") @@ -335,7 +364,8 @@ class TestCase(unittest.TestCase): "dump_path": dump_dir, }, check_output=True, tags=None, - verbose_level=None, check=False) + verbose_level=None, check=False, + list_tasks=False) mock_rmtree.assert_called_once_with(dump_dir) mock_listdir.assert_any_call(dump_dir) mock_read.assert_has_calls([