Debian Codegraph @

lib/ansible/vars/reserved.py

raw
1 # (c) 2017 Ansible By Red Hat
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
22 from ansible.playbook import Play
23 from ansible.playbook.block import Block
24 from ansible.playbook.role import Role
25 from ansible.playbook.task import Task
26 from ansible._internal._templating._engine import TemplateEngine
27 from ansible.utils.display import Display
28
29 display = Display()
30
31
32 def get_reserved_names(include_private: bool = True) -> set[str]:
33 """ this function returns the list of reserved names associated with play objects"""
34
35 public = set(TemplateEngine().environment.globals.keys())
36 private = set()
37
38 # FIXME: find a way to 'not hardcode', possibly need role deps/includes
39 class_list = [Play, Role, Block, Task]
40
41 for aclass in class_list:
42 # build ordered list to loop over and dict with attributes
43 for name, attr in aclass.fattributes.items():
44 if attr.private:
45 private.add(name)
46 else:
47 public.add(name)
48
49 # local_action is implicit with action
50 if 'action' in public:
51 public.add('local_action')
52
53 # loop implies with_
54 # FIXME: remove after with_ is not only deprecated but removed
55 if 'loop' in private or 'loop' in public:
56 public.add('with_')
57
58 if include_private:
59 result = public.union(private)
60 else:
61 result = public
62
63 result.discard('gather_subset')
64
65 return result
66
67
68 def warn_if_reserved(myvars: c.Iterable[str], additional: c.Iterable[str] | None = None) -> None:
69 """Issue a warning for any variable which conflicts with an internally reserved name."""
70 if additional is None:
71 reserved = _RESERVED_NAMES
72 else:
73 reserved = _RESERVED_NAMES.union(additional)
74
75 varnames = set(myvars)
76 varnames.discard('vars') # we add this one internally, so safe to ignore
77
78 if conflicts := varnames.intersection(reserved):
79 # Ensure the varname used for obj is the tagged one from myvars and not the untagged one from reserved.
80 # This can occur because tags do not affect value equality, and intersection can return values from either the left or right side.
81 for varname in (name for name in myvars if name in conflicts):
82 display.warning(f'Found variable using reserved name {varname!r}.', obj=varname)
83
84
85 def is_reserved_name(name: str) -> bool:
86 return name in _RESERVED_NAMES
87
88
89 _RESERVED_NAMES = frozenset(get_reserved_names())