From 0209685f8df83da13df162dad22027dc3e1d6da4 Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Fri, 23 Jun 2017 15:56:16 -0400 Subject: [PATCH] check options exist before forcing assignment --- lib/ansible/utils/vars.py | 36 +++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/lib/ansible/utils/vars.py b/lib/ansible/utils/vars.py index 8a83f82a5fa..3d0ce388bb8 100644 --- a/lib/ansible/utils/vars.py +++ b/lib/ansible/utils/vars.py @@ -121,23 +121,24 @@ def merge_hash(a, b): def load_extra_vars(loader, options): extra_vars = {} - for extra_vars_opt in options.extra_vars: - data = None - extra_vars_opt = to_text(extra_vars_opt, errors='surrogate_or_strict') - 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 and extra_vars_opt[0] in u'[{': - # Arguments as YAML - data = loader.load(extra_vars_opt) - else: - # Arguments as Key-value - data = parse_kv(extra_vars_opt) + if hasattr(options, 'extra_vars'): + for extra_vars_opt in options.extra_vars: + data = None + extra_vars_opt = to_text(extra_vars_opt, errors='surrogate_or_strict') + 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 and extra_vars_opt[0] in 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) return extra_vars @@ -146,7 +147,8 @@ def load_options_vars(options, version): options_vars = {} # For now only return check mode, but we can easily return more # options if we need variables for them - options_vars['ansible_check_mode'] = options.check + if hasattr(options, 'check'): + options_vars['ansible_check_mode'] = options.check options_vars['ansible_version'] = version return options_vars