| 1 |
# (c) 2012-2014, Michael DeHaan <[email protected]> |
| 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 |
import collections.abc as c |
| 21 |
import typing as t |
| 22 |
|
| 23 |
from ansible.module_utils._internal import _datatag |
| 24 |
|
| 25 |
if t.TYPE_CHECKING: |
| 26 |
from ansible.inventory.manager import InventoryManager |
| 27 |
from ansible.parsing.dataloader import DataLoader |
| 28 |
from ansible._internal._templating._jinja_common import Marker |
| 29 |
from ansible.vars.manager import VariableManager |
| 30 |
|
| 31 |
|
| 32 |
__all__ = ['HostVars', 'HostVarsVars'] |
| 33 |
|
| 34 |
|
| 35 |
class HostVars(c.Mapping): |
| 36 |
"""A read-only wrapper to enable on-demand templating of a specific host's variables under that host's variable context.""" |
| 37 |
def __init__(self, inventory: InventoryManager, variable_manager: VariableManager, loader: DataLoader) -> None: |
| 38 |
self._inventory = inventory |
| 39 |
self._loader = loader |
| 40 |
self._variable_manager = variable_manager |
| 41 |
|
| 42 |
variable_manager._hostvars = self |
| 43 |
|
| 44 |
def raw_get(self, host_name: str) -> dict[str, t.Any] | Marker: |
| 45 |
""" |
| 46 |
Similar to __getitem__, however the returned data is not run through |
| 47 |
the templating engine to expand variables in the hostvars. |
| 48 |
""" |
| 49 |
# does not use inventory.hosts, so it can create localhost on demand |
| 50 |
host = self._inventory.get_host(host_name) |
| 51 |
|
| 52 |
if host is None: |
| 53 |
from ansible._internal._templating import _jinja_bits |
| 54 |
|
| 55 |
return _jinja_bits._undef(f"hostvars[{host_name!r}]") |
| 56 |
|
| 57 |
return self._variable_manager.get_vars(host=host, include_hostvars=False) |
| 58 |
|
| 59 |
def __getitem__(self, key: str) -> HostVarsVars | Marker: |
| 60 |
data = self.raw_get(key) |
| 61 |
|
| 62 |
from ansible._internal._templating import _jinja_bits |
| 63 |
|
| 64 |
if isinstance(data, _jinja_bits.Marker): |
| 65 |
return data |
| 66 |
|
| 67 |
return HostVarsVars(data, loader=self._loader, host=key) |
| 68 |
|
| 69 |
def __contains__(self, item: object) -> bool: |
| 70 |
# does not use inventory.hosts, so it can create localhost on demand |
| 71 |
return self._inventory.get_host(item) is not None |
| 72 |
|
| 73 |
def __iter__(self) -> t.Iterator[str]: |
| 74 |
yield from self._inventory.hosts |
| 75 |
|
| 76 |
def __len__(self) -> int: |
| 77 |
return len(self._inventory.hosts) |
| 78 |
|
| 79 |
def __deepcopy__(self, memo: t.Any) -> HostVars: |
| 80 |
# this object may be stored in a var dict that is itself deep copied, but since the underlying data |
| 81 |
# is supposed to be immutable, we don't need to actually copy the data |
| 82 |
return self |
| 83 |
|
| 84 |
|
| 85 |
class HostVarsVars(c.Mapping): |
| 86 |
"""A read-only view of a specific host's vars that will template on access under that host's variable context.""" |
| 87 |
|
| 88 |
def __init__(self, variables: dict[str, t.Any], loader: DataLoader | None, host: str) -> None: |
| 89 |
from ansible._internal._templating import _engine |
| 90 |
|
| 91 |
self._vars = variables |
| 92 |
self._templar = _engine.TemplateEngine(variables=variables, loader=loader) |
| 93 |
self._host = host |
| 94 |
|
| 95 |
def __getitem__(self, key: str) -> t.Any: |
| 96 |
return self._templar.template(self._vars[key]) |
| 97 |
|
| 98 |
def __contains__(self, item: object) -> bool: |
| 99 |
return item in self._vars |
| 100 |
|
| 101 |
def __iter__(self) -> t.Iterator[str]: |
| 102 |
yield from self._vars |
| 103 |
|
| 104 |
def __len__(self) -> int: |
| 105 |
return len(self._vars) |
| 106 |
|
| 107 |
def __repr__(self) -> str: |
| 108 |
return f'HostVarsVars({self._host=!r}, ...)' |
| 109 |
|
| 110 |
def __deepcopy__(self, memo: t.Any) -> HostVarsVars: |
| 111 |
# this may be stored in a var dict that is itself deep copied, but since the underlying data |
| 112 |
# is supposed to be immutable, we don't need to actually copy the data |
| 113 |
return self |
| 114 |
|
| 115 |
|
| 116 |
# DTFIX-FUTURE: is there a better way to add this to the ignorable types in the module_utils code |
| 117 |
_datatag._untaggable_types.update({HostVars, HostVarsVars}) |