Пример #1
0
def ansible_config_get(key: str, kind: Type[Any] = str) -> Union[str, List[str], None]:
    """Return configuration item from ansible config."""
    env = os.environ.copy()
    # Avoid possible ANSI garbage
    env["ANSIBLE_FORCE_COLOR"] = "0"
    # Avoid our own override as this prevents returning system paths.
    colpathvar = ansible_collections_path()
    if colpathvar in env:
        env.pop(colpathvar)

    config = subprocess.check_output(
        ["ansible-config", "dump"], universal_newlines=True, env=env
    )

    if kind == str:
        result = re.search(rf"^{key}.* = (.*)$", config, re.MULTILINE)
        if result:
            return result.groups()[0]
    elif kind == list:
        result = re.search(rf"^{key}.* = (\[.*\])$", config, re.MULTILINE)
        if result:
            val = eval(result.groups()[0])  # pylint: disable=eval-used
            if not isinstance(val, list):
                raise RuntimeError(f"Unexpected data read for {key}: {val}")
            return val
    else:
        raise RuntimeError("Unknown data type.")
    return None
Пример #2
0
def _prepare_ansible_paths() -> None:
    """Configure Ansible environment variables."""
    library_paths: List[str] = []
    roles_path: List[str] = []

    if 'ANSIBLE_ROLES_PATH' in os.environ:
        roles_path = os.environ['ANSIBLE_ROLES_PATH'].split(':')
    if 'ANSIBLE_LIBRARY' in os.environ:
        library_paths = os.environ['ANSIBLE_LIBRARY'].split(':')

    if os.path.exists(
            "plugins/modules") and "plugins/modules" not in library_paths:
        library_paths.append("plugins/modules")

    if os.path.exists(".cache/collections"):
        collection_list.append(".cache/collections")
    if os.path.exists(".cache/modules"):
        library_paths.append(".cache/modules")
    if os.path.exists("roles"):
        roles_path.append("roles")
    if os.path.exists(".cache/roles"):
        roles_path.append(".cache/roles")

    _update_env('ANSIBLE_LIBRARY', library_paths)
    _update_env(ansible_collections_path(), collection_list)
    _update_env('ANSIBLE_ROLES_PATH', roles_path)
Пример #3
0
def _prepare_ansible_paths() -> None:
    """Configure Ansible environment variables."""
    library_paths: List[str] = []
    roles_path: List[str] = []

    for path_list, path in (
        (library_paths, "plugins/modules"),
        (library_paths, f"{options.project_dir}/.cache/modules"),
        (collection_list, f"{options.project_dir}/.cache/collections"),
        (roles_path, "roles"),
        (roles_path, f"{options.project_dir}/.cache/roles"),
    ):
        if path not in path_list and os.path.exists(path):
            path_list.append(path)

    _update_env('ANSIBLE_LIBRARY', library_paths)
    _update_env(ansible_collections_path(), collection_list)
    _update_env('ANSIBLE_ROLES_PATH', roles_path, default=ANSIBLE_DEFAULT_ROLES_PATH)
Пример #4
0
def _prepare_ansible_paths() -> None:
    """Configure Ansible environment variables."""
    library_paths: List[str] = []
    roles_path: List[str] = []

    if 'ANSIBLE_ROLES_PATH' in os.environ:
        roles_path = os.environ['ANSIBLE_ROLES_PATH'].split(':')
    if 'ANSIBLE_LIBRARY' in os.environ:
        library_paths = os.environ['ANSIBLE_LIBRARY'].split(':')

    for path_list, path in (
        (library_paths, "plugins/modules"),
        (library_paths, ".cache/modules"),
        (collection_list, ".cache/collections"),
        (roles_path, "roles"),
        (roles_path, ".cache/roles"),
    ):
        if path not in path_list and os.path.exists(path):
            path_list.append(path)

    _update_env('ANSIBLE_LIBRARY', library_paths)
    _update_env(ansible_collections_path(), collection_list)
    _update_env('ANSIBLE_ROLES_PATH', roles_path)