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): """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) env = os.environ.copy() vault.update_environment(parsed_args, env) # If the configuration path has been specified via --config-path, ensure # the environment variable is set, so that it can be referenced by # playbooks. env.setdefault(CONFIG_PATH_ENV, parsed_args.config_path) try: 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)
def run(parsed_args, command, inventory_filename, extra_vars=None, tags=None, quiet=False, verbose_level=None, extra_args=None, limit=None): """Run a Kolla Ansible command.""" _validate_args(parsed_args, inventory_filename) cmd = build_args(parsed_args, command, inventory_filename=inventory_filename, extra_vars=extra_vars, tags=tags, verbose_level=verbose_level, extra_args=extra_args, limit=limit) env = os.environ.copy() vault.update_environment(parsed_args, env) try: utils.run_command(" ".join(cmd), quiet=quiet, shell=True, env=env) except subprocess.CalledProcessError as e: LOG.error("kolla-ansible %s exited %d", command, e.returncode) sys.exit(e.returncode)
def test_update_environment_no_vault(self): parser = argparse.ArgumentParser() vault.add_args(parser) parsed_args = parser.parse_args([]) env = {} vault.update_environment(parsed_args, env) self.assertEqual({}, env)
def test_update_environment_prompt(self, mock_ask): mock_ask.return_value = "test-pass" parser = argparse.ArgumentParser() vault.add_args(parser) parsed_args = parser.parse_args(["--ask-vault-pass"]) env = {} vault.update_environment(parsed_args, env) self.assertEqual({"KAYOBE_VAULT_PASSWORD": "******"}, env) mock_ask.assert_called_once_with()
def test_update_environment_file(self, mock_read): mock_read.return_value = "test-pass" parser = argparse.ArgumentParser() vault.add_args(parser) args = ["--vault-password-file", "/path/to/file"] parsed_args = parser.parse_args(args) env = {} vault.update_environment(parsed_args, env) self.assertEqual({"KAYOBE_VAULT_PASSWORD": "******"}, env) mock_read.assert_called_once_with("/path/to/file")
def _get_environment(parsed_args): """Return an environment dict for executing an Ansible playbook.""" env = os.environ.copy() vault.update_environment(parsed_args, env) # If the configuration path has been specified via --config-path, ensure # the environment variable is set, so that it can be referenced by # playbooks. env.setdefault(CONFIG_PATH_ENV, parsed_args.config_path) # If a custom Ansible configuration file exists, use it. ansible_cfg_path = os.path.join(parsed_args.config_path, "ansible.cfg") if utils.is_readable_file(ansible_cfg_path)["result"]: env.setdefault("ANSIBLE_CONFIG", ansible_cfg_path) return env
def _get_environment(parsed_args): """Return an environment dict for executing Kolla Ansible.""" env = os.environ.copy() vault.update_environment(parsed_args, env) # If a custom Ansible configuration file exists, use it. Allow # etc/kayobe/kolla/ansible.cfg or etc/kayobe/ansible.cfg. ansible_cfg_path = os.path.join(parsed_args.config_path, "kolla", "ansible.cfg") if utils.is_readable_file(ansible_cfg_path)["result"]: env.setdefault("ANSIBLE_CONFIG", ansible_cfg_path) else: ansible_cfg_path = os.path.join(parsed_args.config_path, "ansible.cfg") if utils.is_readable_file(ansible_cfg_path)["result"]: env.setdefault("ANSIBLE_CONFIG", ansible_cfg_path) return env
def _get_environment(parsed_args): """Return an environment dict for executing Kolla Ansible.""" env = os.environ.copy() vault.update_environment(parsed_args, env) # If a custom Ansible configuration file exists, use it. Allow # etc/kayobe/kolla/ansible.cfg or etc/kayobe/ansible.cfg. ansible_cfg_path = os.path.join(parsed_args.config_path, "kolla", "ansible.cfg") if utils.is_readable_file(ansible_cfg_path)["result"]: env.setdefault("ANSIBLE_CONFIG", ansible_cfg_path) else: ansible_cfg_path = os.path.join(parsed_args.config_path, "ansible.cfg") if utils.is_readable_file(ansible_cfg_path)["result"]: env.setdefault("ANSIBLE_CONFIG", ansible_cfg_path) # kolla-ansible allows passing additional arguments to ansible-playbook via # EXTRA_OPTS. if parsed_args.check or parsed_args.diff: extra_opts = env.setdefault("EXTRA_OPTS", "") if parsed_args.check and "--check" not in extra_opts: env["EXTRA_OPTS"] += " --check" if parsed_args.diff and "--diff" not in extra_opts: env["EXTRA_OPTS"] += " --diff" return env