| 1 |
# Copyright 2017 RedHat, inc |
| 2 |
# |
| 3 |
# This file is part of Ansible |
| 4 |
# |
| 5 |
# Ansible is free software: you can redistribute it and/or modify |
| 6 |
# it under the terms of the GNU General Public License as published by |
| 7 |
# the Free Software Foundation, either version 3 of the License, or |
| 8 |
# (at your option) any later version. |
| 9 |
# |
| 10 |
# Ansible is distributed in the hope that it will be useful, |
| 11 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
# GNU General Public License for more details. |
| 14 |
# |
| 15 |
# You should have received a copy of the GNU General Public License |
| 16 |
# along with Ansible. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
############################################# |
| 18 |
from __future__ import annotations |
| 19 |
|
| 20 |
DOCUMENTATION = """ |
| 21 |
name: host_group_vars |
| 22 |
version_added: "2.4" |
| 23 |
short_description: In charge of loading group_vars and host_vars |
| 24 |
requirements: |
| 25 |
- Enabled in configuration |
| 26 |
description: |
| 27 |
- Loads YAML vars into corresponding groups/hosts in group_vars/ and host_vars/ directories. |
| 28 |
- Files are restricted by extension to one of .yaml, .json, .yml or no extension. |
| 29 |
- Hidden (starting with '.') and backup (ending with '~') files and directories are ignored. |
| 30 |
- Only applies to inventory sources that are existing paths. |
| 31 |
- Starting in 2.10, this plugin requires enabling and is enabled by default. |
| 32 |
notes: |
| 33 |
- This plugin caches applicable paths and file content, so creating, modifying, or removing C(host_vars) and C(group_vars) files mid-play |
| 34 |
for hosts or groups that have already been targeted has no effect on the current or any subsequent plays. |
| 35 |
options: |
| 36 |
stage: |
| 37 |
ini: |
| 38 |
- key: stage |
| 39 |
section: vars_host_group_vars |
| 40 |
env: |
| 41 |
- name: ANSIBLE_VARS_PLUGIN_STAGE |
| 42 |
_valid_extensions: |
| 43 |
default: [".yml", ".yaml", ".json"] |
| 44 |
description: |
| 45 |
- "Check all of these extensions when looking for 'variable' files which should be YAML or JSON or vaulted versions of these." |
| 46 |
- 'This affects vars_files, include_vars, inventory and vars plugins among others.' |
| 47 |
env: |
| 48 |
- name: ANSIBLE_YAML_FILENAME_EXT |
| 49 |
ini: |
| 50 |
- key: yaml_valid_extensions |
| 51 |
section: defaults |
| 52 |
type: list |
| 53 |
elements: string |
| 54 |
extends_documentation_fragment: |
| 55 |
- vars_plugin_staging |
| 56 |
""" |
| 57 |
|
| 58 |
import os |
| 59 |
from ansible.errors import AnsibleError, AnsibleParserError |
| 60 |
from ansible.module_utils.common.text.converters import to_native |
| 61 |
from ansible.plugins.vars import BaseVarsPlugin |
| 62 |
from ansible.utils.path import basedir |
| 63 |
from ansible.inventory.group import InventoryObjectType |
| 64 |
from ansible.utils.vars import combine_vars |
| 65 |
|
| 66 |
CANONICAL_PATHS = {} # type: dict[str, str] |
| 67 |
FOUND = {} # type: dict[str, list[str]] |
| 68 |
NAK = set() # type: set[str] |
| 69 |
|
| 70 |
|
| 71 |
class VarsModule(BaseVarsPlugin): |
| 72 |
|
| 73 |
REQUIRES_ENABLED = True |
| 74 |
is_stateless = True |
| 75 |
|
| 76 |
def load_found_files(self, loader, data, found_files): |
| 77 |
for found in found_files: |
| 78 |
new_data = loader.load_from_file(found, cache='all', unsafe=True, trusted_as_template=True) |
| 79 |
if new_data: # ignore empty files |
| 80 |
try: |
| 81 |
data = combine_vars(data, new_data) |
| 82 |
except AnsibleError as e: |
| 83 |
raise AnsibleParserError(f"Could not process {found!r}.") from e |
| 84 |
return data |
| 85 |
|
| 86 |
def get_vars(self, loader, path, entities, cache=True): |
| 87 |
""" parses the inventory file """ |
| 88 |
|
| 89 |
if not isinstance(entities, list): |
| 90 |
entities = [entities] |
| 91 |
|
| 92 |
# realpath is expensive |
| 93 |
try: |
| 94 |
realpath_basedir = CANONICAL_PATHS[path] |
| 95 |
except KeyError: |
| 96 |
CANONICAL_PATHS[path] = realpath_basedir = os.path.realpath(basedir(path)) |
| 97 |
|
| 98 |
data = {} |
| 99 |
for entity in entities: |
| 100 |
try: |
| 101 |
entity_name = entity.name |
| 102 |
except AttributeError: |
| 103 |
raise AnsibleParserError("Supplied entity must be Host or Group, got %s instead" % (type(entity))) |
| 104 |
|
| 105 |
try: |
| 106 |
first_char = entity_name[0] |
| 107 |
except (TypeError, IndexError, KeyError): |
| 108 |
raise AnsibleParserError("Supplied entity must be Host or Group, got %s instead" % (type(entity))) |
| 109 |
|
| 110 |
# avoid 'chroot' type inventory hostnames /path/to/chroot |
| 111 |
if first_char != os.path.sep: |
| 112 |
try: |
| 113 |
found_files = [] |
| 114 |
# load vars |
| 115 |
try: |
| 116 |
entity_type = entity.base_type |
| 117 |
except AttributeError: |
| 118 |
raise AnsibleParserError("Supplied entity must be Host or Group, got %s instead" % (type(entity))) |
| 119 |
|
| 120 |
if entity_type is InventoryObjectType.HOST: |
| 121 |
subdir = 'host_vars' |
| 122 |
elif entity_type is InventoryObjectType.GROUP: |
| 123 |
subdir = 'group_vars' |
| 124 |
else: |
| 125 |
raise AnsibleParserError("Supplied entity must be Host or Group, got %s instead" % (type(entity))) |
| 126 |
|
| 127 |
opath = os.path.join(realpath_basedir, subdir) |
| 128 |
key = '%s.%s' % (entity_name, opath) |
| 129 |
|
| 130 |
if cache: |
| 131 |
if opath in NAK: |
| 132 |
continue |
| 133 |
if key in FOUND: |
| 134 |
data = self.load_found_files(loader, data, FOUND[key]) |
| 135 |
continue |
| 136 |
if os.path.isdir(opath): |
| 137 |
self._display.debug("\tprocessing dir %s" % opath) |
| 138 |
FOUND[key] = found_files = loader.find_vars_files(opath, entity_name) |
| 139 |
elif not os.path.exists(opath): |
| 140 |
# cache missing dirs so we don't have to keep looking for things beneath the |
| 141 |
NAK.add(opath) |
| 142 |
else: |
| 143 |
self._display.warning("Found %s that is not a directory, skipping: %s" % (subdir, opath)) |
| 144 |
# cache non-directory matches |
| 145 |
NAK.add(opath) |
| 146 |
|
| 147 |
data = self.load_found_files(loader, data, found_files) |
| 148 |
|
| 149 |
except Exception as e: |
| 150 |
raise AnsibleParserError(to_native(e)) |
| 151 |
return data |