|
|
@ -5,13 +5,19 @@ from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import ast
|
|
|
|
import ast
|
|
|
|
|
|
|
|
from collections.abc import Mapping
|
|
|
|
from itertools import islice, chain
|
|
|
|
from itertools import islice, chain
|
|
|
|
from types import GeneratorType
|
|
|
|
from types import GeneratorType
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from ansible.module_utils.common.collections import is_sequence
|
|
|
|
from ansible.module_utils.common.text.converters import to_text
|
|
|
|
from ansible.module_utils.common.text.converters import to_text
|
|
|
|
from ansible.module_utils.six import string_types
|
|
|
|
from ansible.module_utils.six import string_types
|
|
|
|
from ansible.parsing.yaml.objects import AnsibleVaultEncryptedUnicode
|
|
|
|
from ansible.parsing.yaml.objects import AnsibleVaultEncryptedUnicode
|
|
|
|
from ansible.utils.native_jinja import NativeJinjaText
|
|
|
|
from ansible.utils.native_jinja import NativeJinjaText
|
|
|
|
|
|
|
|
from ansible.utils.unsafe_proxy import wrap_var
|
|
|
|
|
|
|
|
import ansible.module_utils.compat.typing as t
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from jinja2.runtime import StrictUndefined
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_JSON_MAP = {
|
|
|
|
_JSON_MAP = {
|
|
|
@ -28,6 +34,40 @@ class Json2Python(ast.NodeTransformer):
|
|
|
|
return ast.Constant(value=_JSON_MAP[node.id])
|
|
|
|
return ast.Constant(value=_JSON_MAP[node.id])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _is_unsafe(value: t.Any) -> bool:
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
Our helper function, which will also recursively check dict and
|
|
|
|
|
|
|
|
list entries due to the fact that they may be repr'd and contain
|
|
|
|
|
|
|
|
a key or value which contains jinja2 syntax and would otherwise
|
|
|
|
|
|
|
|
lose the AnsibleUnsafe value.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
to_check = [value]
|
|
|
|
|
|
|
|
seen = set()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
while True:
|
|
|
|
|
|
|
|
if not to_check:
|
|
|
|
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
val = to_check.pop(0)
|
|
|
|
|
|
|
|
val_id = id(val)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if val_id in seen:
|
|
|
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
seen.add(val_id)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if isinstance(val, AnsibleUndefined):
|
|
|
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
if isinstance(val, Mapping):
|
|
|
|
|
|
|
|
to_check.extend(val.keys())
|
|
|
|
|
|
|
|
to_check.extend(val.values())
|
|
|
|
|
|
|
|
elif is_sequence(val):
|
|
|
|
|
|
|
|
to_check.extend(val)
|
|
|
|
|
|
|
|
elif getattr(val, '__UNSAFE__', False):
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def ansible_eval_concat(nodes):
|
|
|
|
def ansible_eval_concat(nodes):
|
|
|
|
"""Return a string of concatenated compiled nodes. Throw an undefined error
|
|
|
|
"""Return a string of concatenated compiled nodes. Throw an undefined error
|
|
|
|
if any of the nodes is undefined.
|
|
|
|
if any of the nodes is undefined.
|
|
|
@ -43,17 +83,28 @@ def ansible_eval_concat(nodes):
|
|
|
|
if not head:
|
|
|
|
if not head:
|
|
|
|
return ''
|
|
|
|
return ''
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
unsafe = False
|
|
|
|
|
|
|
|
|
|
|
|
if len(head) == 1:
|
|
|
|
if len(head) == 1:
|
|
|
|
out = head[0]
|
|
|
|
out = head[0]
|
|
|
|
|
|
|
|
|
|
|
|
if isinstance(out, NativeJinjaText):
|
|
|
|
if isinstance(out, NativeJinjaText):
|
|
|
|
return out
|
|
|
|
return out
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
unsafe = _is_unsafe(out)
|
|
|
|
out = to_text(out)
|
|
|
|
out = to_text(out)
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
if isinstance(nodes, GeneratorType):
|
|
|
|
if isinstance(nodes, GeneratorType):
|
|
|
|
nodes = chain(head, nodes)
|
|
|
|
nodes = chain(head, nodes)
|
|
|
|
out = ''.join([to_text(v) for v in nodes])
|
|
|
|
|
|
|
|
|
|
|
|
out_values = []
|
|
|
|
|
|
|
|
for v in nodes:
|
|
|
|
|
|
|
|
if not unsafe and _is_unsafe(v):
|
|
|
|
|
|
|
|
unsafe = True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
out_values.append(to_text(v))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
out = ''.join(out_values)
|
|
|
|
|
|
|
|
|
|
|
|
# if this looks like a dictionary, list or bool, convert it to such
|
|
|
|
# if this looks like a dictionary, list or bool, convert it to such
|
|
|
|
if out.startswith(('{', '[')) or out in ('True', 'False'):
|
|
|
|
if out.startswith(('{', '[')) or out in ('True', 'False'):
|
|
|
@ -68,6 +119,9 @@ def ansible_eval_concat(nodes):
|
|
|
|
except (TypeError, ValueError, SyntaxError, MemoryError):
|
|
|
|
except (TypeError, ValueError, SyntaxError, MemoryError):
|
|
|
|
pass
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if unsafe:
|
|
|
|
|
|
|
|
out = wrap_var(out)
|
|
|
|
|
|
|
|
|
|
|
|
return out
|
|
|
|
return out
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -78,7 +132,19 @@ def ansible_concat(nodes):
|
|
|
|
|
|
|
|
|
|
|
|
Used in Templar.template() when jinja2_native=False and convert_data=False.
|
|
|
|
Used in Templar.template() when jinja2_native=False and convert_data=False.
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
return ''.join([to_text(v) for v in nodes])
|
|
|
|
unsafe = False
|
|
|
|
|
|
|
|
values = []
|
|
|
|
|
|
|
|
for v in nodes:
|
|
|
|
|
|
|
|
if not unsafe and _is_unsafe(v):
|
|
|
|
|
|
|
|
unsafe = True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
values.append(to_text(v))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
out = ''.join(values)
|
|
|
|
|
|
|
|
if unsafe:
|
|
|
|
|
|
|
|
out = wrap_var(out)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return out
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def ansible_native_concat(nodes):
|
|
|
|
def ansible_native_concat(nodes):
|
|
|
@ -95,6 +161,8 @@ def ansible_native_concat(nodes):
|
|
|
|
if not head:
|
|
|
|
if not head:
|
|
|
|
return None
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
unsafe = False
|
|
|
|
|
|
|
|
|
|
|
|
if len(head) == 1:
|
|
|
|
if len(head) == 1:
|
|
|
|
out = head[0]
|
|
|
|
out = head[0]
|
|
|
|
|
|
|
|
|
|
|
@ -115,10 +183,21 @@ def ansible_native_concat(nodes):
|
|
|
|
# short-circuit literal_eval for anything other than strings
|
|
|
|
# short-circuit literal_eval for anything other than strings
|
|
|
|
if not isinstance(out, string_types):
|
|
|
|
if not isinstance(out, string_types):
|
|
|
|
return out
|
|
|
|
return out
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
unsafe = _is_unsafe(out)
|
|
|
|
|
|
|
|
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
if isinstance(nodes, GeneratorType):
|
|
|
|
if isinstance(nodes, GeneratorType):
|
|
|
|
nodes = chain(head, nodes)
|
|
|
|
nodes = chain(head, nodes)
|
|
|
|
out = ''.join([to_text(v) for v in nodes])
|
|
|
|
|
|
|
|
|
|
|
|
out_values = []
|
|
|
|
|
|
|
|
for v in nodes:
|
|
|
|
|
|
|
|
if not unsafe and _is_unsafe(v):
|
|
|
|
|
|
|
|
unsafe = True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
out_values.append(to_text(v))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
out = ''.join(out_values)
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
evaled = ast.literal_eval(
|
|
|
|
evaled = ast.literal_eval(
|
|
|
@ -128,10 +207,45 @@ def ansible_native_concat(nodes):
|
|
|
|
ast.parse(out, mode='eval')
|
|
|
|
ast.parse(out, mode='eval')
|
|
|
|
)
|
|
|
|
)
|
|
|
|
except (TypeError, ValueError, SyntaxError, MemoryError):
|
|
|
|
except (TypeError, ValueError, SyntaxError, MemoryError):
|
|
|
|
|
|
|
|
if unsafe:
|
|
|
|
|
|
|
|
out = wrap_var(out)
|
|
|
|
|
|
|
|
|
|
|
|
return out
|
|
|
|
return out
|
|
|
|
|
|
|
|
|
|
|
|
if isinstance(evaled, string_types):
|
|
|
|
if isinstance(evaled, string_types):
|
|
|
|
quote = out[0]
|
|
|
|
quote = out[0]
|
|
|
|
return f'{quote}{evaled}{quote}'
|
|
|
|
evaled = f'{quote}{evaled}{quote}'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if unsafe:
|
|
|
|
|
|
|
|
evaled = wrap_var(evaled)
|
|
|
|
|
|
|
|
|
|
|
|
return evaled
|
|
|
|
return evaled
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class AnsibleUndefined(StrictUndefined):
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
A custom Undefined class, which returns further Undefined objects on access,
|
|
|
|
|
|
|
|
rather than throwing an exception.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __getattr__(self, name):
|
|
|
|
|
|
|
|
if name == '__UNSAFE__':
|
|
|
|
|
|
|
|
# AnsibleUndefined should never be assumed to be unsafe
|
|
|
|
|
|
|
|
# This prevents ``hasattr(val, '__UNSAFE__')`` from evaluating to ``True``
|
|
|
|
|
|
|
|
raise AttributeError(name)
|
|
|
|
|
|
|
|
# Return original Undefined object to preserve the first failure context
|
|
|
|
|
|
|
|
return self
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def __getitem__(self, key):
|
|
|
|
|
|
|
|
# Return original Undefined object to preserve the first failure context
|
|
|
|
|
|
|
|
return self
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
|
|
|
|
return 'AnsibleUndefined(hint={0!r}, obj={1!r}, name={2!r})'.format(
|
|
|
|
|
|
|
|
self._undefined_hint,
|
|
|
|
|
|
|
|
self._undefined_obj,
|
|
|
|
|
|
|
|
self._undefined_name
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def __contains__(self, item):
|
|
|
|
|
|
|
|
# Return original Undefined object to preserve the first failure context
|
|
|
|
|
|
|
|
return self
|
|
|
|