Skip to content

Commit

Permalink
centralize and complete the internal static vars (#82872) (#82925)
Browse files Browse the repository at this point in the history
* centralize and complete the internal static vars

These vars are internal and should not be overridden nor templated
from inventory nor hostvars.

(cherry picked from commit 8704b9f)
  • Loading branch information
bcoca committed Apr 10, 2024
1 parent 85697be commit f8162c1
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 43 deletions.
3 changes: 3 additions & 0 deletions changelogs/fragments/internal_static_vars.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
bugfixes:
- Consolidated the list of internal static vars, centralized them as constant and completed from some missing entries.
- Slight optimization to hostvars (instantiate template only once per host, vs per call to var).
22 changes: 1 addition & 21 deletions lib/ansible/cli/inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,6 @@

display = Display()

INTERNAL_VARS = frozenset(['ansible_diff_mode',
'ansible_config_file',
'ansible_facts',
'ansible_forks',
'ansible_inventory_sources',
'ansible_limit',
'ansible_playbook_python',
'ansible_run_tags',
'ansible_skip_tags',
'ansible_verbosity',
'ansible_version',
'inventory_dir',
'inventory_file',
'inventory_hostname',
'inventory_hostname_short',
'groups',
'group_names',
'omit',
'playbook_dir', ])


class InventoryCLI(CLI):
''' used to display or dump the configured inventory as Ansible sees it '''
Expand Down Expand Up @@ -247,7 +227,7 @@ def _get_group(self, gname):
@staticmethod
def _remove_internal(dump):

for internal in INTERNAL_VARS:
for internal in C.INTERNAL_STATIC_VARS:
if internal in dump:
del dump[internal]

Expand Down
40 changes: 40 additions & 0 deletions lib/ansible/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,46 @@ def __getitem__(self, y):
DOCUMENTABLE_PLUGINS = CONFIGURABLE_PLUGINS + ('module', 'strategy', 'test', 'filter')
IGNORE_FILES = ("COPYING", "CONTRIBUTING", "LICENSE", "README", "VERSION", "GUIDELINES", "MANIFEST", "Makefile") # ignore during module search
INTERNAL_RESULT_KEYS = ('add_host', 'add_group')
INTERNAL_STATIC_VARS = frozenset(
[
"ansible_async_path",
"ansible_collection_name",
"ansible_config_file",
"ansible_dependent_role_names",
"ansible_diff_mode",
"ansible_config_file",
"ansible_facts",
"ansible_forks",
"ansible_inventory_sources",
"ansible_limit",
"ansible_play_batch",
"ansible_play_hosts",
"ansible_play_hosts_all",
"ansible_play_role_names",
"ansible_playbook_python",
"ansible_role_name",
"ansible_role_names",
"ansible_run_tags",
"ansible_skip_tags",
"ansible_verbosity",
"ansible_version",
"inventory_dir",
"inventory_file",
"inventory_hostname",
"inventory_hostname_short",
"groups",
"group_names",
"omit",
"hostvars",
"playbook_dir",
"play_hosts",
"role_name",
"role_names",
"role_path",
"role_uuid",
"role_names",
]
)
LOCALHOST = ('127.0.0.1', 'localhost', '::1')
MODULE_REQUIRE_ARGS = tuple(add_internal_fqcns(('command', 'win_command', 'ansible.windows.win_command', 'shell', 'win_shell',
'ansible.windows.win_shell', 'raw', 'script')))
Expand Down
28 changes: 6 additions & 22 deletions lib/ansible/vars/hostvars.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,9 @@

from collections.abc import Mapping

from ansible import constants as C
from ansible.template import Templar, AnsibleUndefined

STATIC_VARS = [
'ansible_version',
'ansible_play_hosts',
'ansible_dependent_role_names',
'ansible_play_role_names',
'ansible_role_names',
'inventory_hostname',
'inventory_hostname_short',
'inventory_file',
'inventory_dir',
'groups',
'group_names',
'omit',
'playbook_dir',
'play_hosts',
'role_names',
'ungrouped',
]

__all__ = ['HostVars', 'HostVarsVars']

Expand Down Expand Up @@ -134,10 +117,12 @@ class HostVarsVars(Mapping):
def __init__(self, variables, loader):
self._vars = variables
self._loader = loader
# NOTE: this only has access to the host's own vars,
# so templates that depend on vars in other scopes will not work.
self._templar = Templar(variables=self._vars, loader=self._loader)

def __getitem__(self, var):
templar = Templar(variables=self._vars, loader=self._loader)
return templar.template(self._vars[var], fail_on_undefined=False, static_vars=STATIC_VARS)
return self._templar.template(self._vars[var], fail_on_undefined=False, static_vars=C.INTERNAL_STATIC_VARS)

def __contains__(self, var):
return (var in self._vars)
Expand All @@ -150,5 +135,4 @@ def __len__(self):
return len(self._vars.keys())

def __repr__(self):
templar = Templar(variables=self._vars, loader=self._loader)
return repr(templar.template(self._vars, fail_on_undefined=False, static_vars=STATIC_VARS))
return repr(self._templar.template(self._vars, fail_on_undefined=False, static_vars=C.INTERNAL_STATIC_VARS))

0 comments on commit f8162c1

Please sign in to comment.