summaryrefslogtreecommitdiff
path: root/lib/ansible/utils/vars.py
diff options
context:
space:
mode:
Diffstat (limited to 'lib/ansible/utils/vars.py')
-rw-r--r--lib/ansible/utils/vars.py118
1 files changed, 51 insertions, 67 deletions
diff --git a/lib/ansible/utils/vars.py b/lib/ansible/utils/vars.py
index a3224c8b..5e21cb36 100644
--- a/lib/ansible/utils/vars.py
+++ b/lib/ansible/utils/vars.py
@@ -29,8 +29,8 @@ from json import dumps
from ansible import constants as C
from ansible import context
from ansible.errors import AnsibleError, AnsibleOptionsError
-from ansible.module_utils.six import string_types, PY3
-from ansible.module_utils._text import to_native, to_text
+from ansible.module_utils.six import string_types
+from ansible.module_utils.common.text.converters import to_native, to_text
from ansible.parsing.splitter import parse_kv
@@ -109,6 +109,8 @@ def merge_hash(x, y, recursive=True, list_merge='replace'):
# except performance)
if x == {} or x == y:
return y.copy()
+ if y == {}:
+ return x
# in the following we will copy elements from y to x, but
# we don't want to modify x, so we create a copy of it
@@ -181,66 +183,67 @@ def merge_hash(x, y, recursive=True, list_merge='replace'):
def load_extra_vars(loader):
- extra_vars = {}
- for extra_vars_opt in context.CLIARGS.get('extra_vars', tuple()):
- data = None
- extra_vars_opt = to_text(extra_vars_opt, errors='surrogate_or_strict')
- if extra_vars_opt is None or not extra_vars_opt:
- continue
- if extra_vars_opt.startswith(u"@"):
- # Argument is a YAML file (JSON is a subset of YAML)
- data = loader.load_from_file(extra_vars_opt[1:])
- elif extra_vars_opt[0] in [u'/', u'.']:
- raise AnsibleOptionsError("Please prepend extra_vars filename '%s' with '@'" % extra_vars_opt)
- elif extra_vars_opt[0] in [u'[', u'{']:
- # Arguments as YAML
- data = loader.load(extra_vars_opt)
- else:
- # Arguments as Key-value
- data = parse_kv(extra_vars_opt)
+ if not getattr(load_extra_vars, 'extra_vars', None):
+ extra_vars = {}
+ for extra_vars_opt in context.CLIARGS.get('extra_vars', tuple()):
+ data = None
+ extra_vars_opt = to_text(extra_vars_opt, errors='surrogate_or_strict')
+ if extra_vars_opt is None or not extra_vars_opt:
+ continue
+
+ if extra_vars_opt.startswith(u"@"):
+ # Argument is a YAML file (JSON is a subset of YAML)
+ data = loader.load_from_file(extra_vars_opt[1:])
+ elif extra_vars_opt[0] in [u'/', u'.']:
+ raise AnsibleOptionsError("Please prepend extra_vars filename '%s' with '@'" % extra_vars_opt)
+ elif extra_vars_opt[0] in [u'[', u'{']:
+ # Arguments as YAML
+ data = loader.load(extra_vars_opt)
+ else:
+ # Arguments as Key-value
+ data = parse_kv(extra_vars_opt)
+
+ if isinstance(data, MutableMapping):
+ extra_vars = combine_vars(extra_vars, data)
+ else:
+ raise AnsibleOptionsError("Invalid extra vars data supplied. '%s' could not be made into a dictionary" % extra_vars_opt)
- if isinstance(data, MutableMapping):
- extra_vars = combine_vars(extra_vars, data)
- else:
- raise AnsibleOptionsError("Invalid extra vars data supplied. '%s' could not be made into a dictionary" % extra_vars_opt)
+ setattr(load_extra_vars, 'extra_vars', extra_vars)
- return extra_vars
+ return load_extra_vars.extra_vars
def load_options_vars(version):
- if version is None:
- version = 'Unknown'
- options_vars = {'ansible_version': version}
- attrs = {'check': 'check_mode',
- 'diff': 'diff_mode',
- 'forks': 'forks',
- 'inventory': 'inventory_sources',
- 'skip_tags': 'skip_tags',
- 'subset': 'limit',
- 'tags': 'run_tags',
- 'verbosity': 'verbosity'}
+ if not getattr(load_options_vars, 'options_vars', None):
+ if version is None:
+ version = 'Unknown'
+ options_vars = {'ansible_version': version}
+ attrs = {'check': 'check_mode',
+ 'diff': 'diff_mode',
+ 'forks': 'forks',
+ 'inventory': 'inventory_sources',
+ 'skip_tags': 'skip_tags',
+ 'subset': 'limit',
+ 'tags': 'run_tags',
+ 'verbosity': 'verbosity'}
- for attr, alias in attrs.items():
- opt = context.CLIARGS.get(attr)
- if opt is not None:
- options_vars['ansible_%s' % alias] = opt
+ for attr, alias in attrs.items():
+ opt = context.CLIARGS.get(attr)
+ if opt is not None:
+ options_vars['ansible_%s' % alias] = opt
- return options_vars
+ setattr(load_options_vars, 'options_vars', options_vars)
+
+ return load_options_vars.options_vars
def _isidentifier_PY3(ident):
if not isinstance(ident, string_types):
return False
- # NOTE Python 3.7 offers str.isascii() so switch over to using it once
- # we stop supporting 3.5 and 3.6 on the controller
- try:
- # Python 2 does not allow non-ascii characters in identifiers so unify
- # the behavior for Python 3
- ident.encode('ascii')
- except UnicodeEncodeError:
+ if not ident.isascii():
return False
if not ident.isidentifier():
@@ -252,26 +255,7 @@ def _isidentifier_PY3(ident):
return True
-def _isidentifier_PY2(ident):
- if not isinstance(ident, string_types):
- return False
-
- if not ident:
- return False
-
- if C.INVALID_VARIABLE_NAMES.search(ident):
- return False
-
- if keyword.iskeyword(ident) or ident in ADDITIONAL_PY2_KEYWORDS:
- return False
-
- return True
-
-
-if PY3:
- isidentifier = _isidentifier_PY3
-else:
- isidentifier = _isidentifier_PY2
+isidentifier = _isidentifier_PY3
isidentifier.__doc__ = """Determine if string is valid identifier.