diff options
author | James Cammarata <jimi@sngx.net> | 2016-11-21 16:57:27 -0600 |
---|---|---|
committer | James Cammarata <jimi@sngx.net> | 2017-02-21 17:44:01 -0600 |
commit | cdd5ed5f3dc21b125a7210e960075df9a987a592 (patch) | |
tree | 437cc910ce7791cb0bd957fd9fe718a927780317 | |
parent | f40e1bc289518decb9be20607248ee6c1d108d06 (diff) | |
download | ansible-cdd5ed5f3dc21b125a7210e960075df9a987a592.tar.gz |
Rework how the Conditional class deals with undefined vars
Previously, the Conditional class did a simple check when an
AnsibleUndefinedVariable error was raised to see if certain strings were
present. This patch tries to be smarter by evaluating the variable contained
in the error string and compared to the defined/not defined conditionals in
the conditional string.
This also modifies the UndefinedError message from HostVars slightly to
match the format returned jinja2 in general, making it easier to match the
error message in the Conditional code.
Fixes #18514
(cherry picked from commit 81aa12eb1b5a2b50ac9234ca0b05c015af88a845)
(cherry picked from commit cfd57fcae27319ad5252d58c466f45366b451f14)
-rw-r--r-- | lib/ansible/playbook/conditional.py | 50 |
1 files changed, 40 insertions, 10 deletions
diff --git a/lib/ansible/playbook/conditional.py b/lib/ansible/playbook/conditional.py index c7c22119a5..dcf9db199d 100644 --- a/lib/ansible/playbook/conditional.py +++ b/lib/ansible/playbook/conditional.py @@ -32,6 +32,7 @@ from ansible.template import Templar LOOKUP_REGEX = re.compile(r'lookup\s*\(') VALID_VAR_REGEX = re.compile("^[_A-Za-z][_a-zA-Z0-9]*$") +DEFINED_REGEX = re.compile(r'(hostvars\[.+\]|[\w_]+)\s+(not\s+is|is|is\s+not)\s+(defined|undefined)') class Conditional: @@ -57,6 +58,18 @@ class Conditional: if not isinstance(value, list): setattr(self, name, [ value ]) + def extract_defined_undefined(self, conditional): + results = [] + + cond = conditional + m = DEFINED_REGEX.search(cond) + while m: + results.append(m.groups()) + cond = cond[m.end():] + m = DEFINED_REGEX.search(cond) + + return results + def evaluate_conditional(self, templar, all_vars): ''' Loops through the conditionals set on this object, returning @@ -151,14 +164,31 @@ class Conditional: else: raise AnsibleError("unable to evaluate conditional: %s" % original) except (AnsibleUndefinedVariable, UndefinedError) as e: - # the templating failed, meaning most likely a - # variable was undefined. If we happened to be - # looking for an undefined variable, return True, - # otherwise fail - if "is undefined" in original: - return True - elif "is defined" in original: - return False - else: - raise AnsibleError("error while evaluating conditional (%s): %s" % (original, e)) + # the templating failed, meaning most likely a variable was undefined. If we happened to be + # looking for an undefined variable, return True, otherwise fail + try: + # first we extract the variable name from the error message + var_name = re.compile(r"'(hostvars\[.+\]|[\w_]+)' is undefined").search(str(e)).groups()[0] + # next we extract all defined/undefined tests from the conditional string + def_undef = self.extract_defined_undefined(conditional) + # then we loop through these, comparing the error variable name against + # each def/undef test we found above. If there is a match, we determine + # whether the logic/state mean the variable should exist or not and return + # the corresponding True/False + for (du_var, logic, state) in def_undef: + # when we compare the var names, normalize quotes because something + # like hostvars['foo'] may be tested against hostvars["foo"] + if var_name.replace("'", '"') == du_var.replace("'", '"'): + # the should exist is a xor test between a negation in the logic portion + # against the state (defined or undefined) + should_exist = ('not' in logic) != (state == 'defined') + if should_exist: + return False + else: + return True + # as nothing above matched the failed var name, re-raise here to + # trigger the AnsibleUndefinedVariable exception again below + raise + except Exception as new_e: + raise AnsibleUndefinedVariable("error while evaluating conditional (%s): %s" % (original, e)) |