diff options
Diffstat (limited to 'lib/ansible/utils/vars.py')
-rw-r--r-- | lib/ansible/utils/vars.py | 118 |
1 files changed, 67 insertions, 51 deletions
diff --git a/lib/ansible/utils/vars.py b/lib/ansible/utils/vars.py index 5e21cb36..a3224c8b 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 -from ansible.module_utils.common.text.converters import to_native, to_text +from ansible.module_utils.six import string_types, PY3 +from ansible.module_utils._text import to_native, to_text from ansible.parsing.splitter import parse_kv @@ -109,8 +109,6 @@ 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 @@ -183,67 +181,66 @@ 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 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 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) - setattr(load_extra_vars, 'extra_vars', extra_vars) + 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) - return load_extra_vars.extra_vars + return extra_vars def load_options_vars(version): - 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'} + 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 - setattr(load_options_vars, 'options_vars', options_vars) - - return load_options_vars.options_vars + return options_vars def _isidentifier_PY3(ident): if not isinstance(ident, string_types): return False - if not ident.isascii(): + # 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: return False if not ident.isidentifier(): @@ -255,7 +252,26 @@ def _isidentifier_PY3(ident): return True -isidentifier = _isidentifier_PY3 +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.__doc__ = """Determine if string is valid identifier. |