Fixing bugs in conditional testing with until and some integration runner tweaks

This commit is contained in:
James Cammarata 2015-12-19 12:49:06 -05:00
parent 5bd01c09d6
commit 9cfa2d7e28
6 changed files with 33 additions and 31 deletions

View file

@ -35,7 +35,7 @@ from ansible.template import Templar
from ansible.utils.encrypt import key_for_hostname
from ansible.utils.listify import listify_lookup_plugin_terms
from ansible.utils.unicode import to_unicode
from ansible.vars.unsafe_proxy import UnsafeProxy
from ansible.vars.unsafe_proxy import UnsafeProxy, wrap_var
try:
from __main__ import display
@ -406,7 +406,7 @@ class TaskExecutor:
# update the local copy of vars with the registered value, if specified,
# or any facts which may have been generated by the module execution
if self._task.register:
vars_copy[self._task.register] = result
vars_copy[self._task.register] = wrap_var(result.copy())
if self._task.async > 0:
# the async_wrapper module returns dumped JSON via its stdout
@ -453,7 +453,7 @@ class TaskExecutor:
if attempt < retries - 1:
cond = Conditional(loader=self._loader)
cond.when = self._task.until
cond.when = [ self._task.until ]
if cond.evaluate_conditional(templar, vars_copy):
break
@ -466,7 +466,7 @@ class TaskExecutor:
# do the final update of the local variables here, for both registered
# values and any facts which may have been created
if self._task.register:
variables[self._task.register] = result
variables[self._task.register] = wrap_var(result)
if 'ansible_facts' in result:
variables.update(result['ansible_facts'])

View file

@ -22,7 +22,7 @@ __metaclass__ = type
from jinja2.exceptions import UndefinedError
from ansible.compat.six import text_type
from ansible.errors import AnsibleError
from ansible.errors import AnsibleError, AnsibleUndefinedVariable
from ansible.playbook.attribute import FieldAttribute
from ansible.template import Templar
@ -89,16 +89,22 @@ class Conditional:
# make sure the templar is using the variables specifed to this method
templar.set_available_variables(variables=all_vars)
conditional = templar.template(conditional)
if not isinstance(conditional, basestring) or conditional == "":
return conditional
try:
conditional = templar.template(conditional)
if not isinstance(conditional, text_type) or conditional == "":
return conditional
# a Jinja2 evaluation that results in something Python can eval!
presented = "{%% if %s %%} True {%% else %%} False {%% endif %%}" % conditional
conditional = templar.template(presented, fail_on_undefined=False)
val = conditional.strip()
if val == presented:
# a Jinja2 evaluation that results in something Python can eval!
presented = "{%% if %s %%} True {%% else %%} False {%% endif %%}" % conditional
conditional = templar.template(presented)
val = conditional.strip()
if val == "True":
return True
elif val == "False":
return False
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,
@ -108,11 +114,5 @@ class Conditional:
elif "is defined" in original:
return False
else:
raise AnsibleError("error while evaluating conditional: %s (%s)" % (original, presented))
elif val == "True":
return True
elif val == "False":
return False
else:
raise AnsibleError("unable to evaluate conditional: %s" % original)
raise AnsibleError("error while evaluating conditional (%s): %s" % (original, e))

View file

@ -82,7 +82,7 @@ class Task(Base, Conditional, Taggable, Become):
_poll = FieldAttribute(isa='int')
_register = FieldAttribute(isa='string')
_retries = FieldAttribute(isa='int', default=3)
_until = FieldAttribute(isa='list')
_until = FieldAttribute(isa='string')
def __init__(self, block=None, role=None, task_include=None):
''' constructors a task, without the Task.load classmethod, it will be pretty blank '''

View file

@ -74,4 +74,4 @@
- name: Fail
shell: 'echo "{{ inventory_hostname }}, Failed" && exit 1'
when: "test_results.rc != 0"
when: "'rc' not in test_results or test_results.rc != 0"

View file

@ -53,6 +53,7 @@
when: ansible_os_family == 'Debian'
- name: update ca certificates
sudo: true
yum: name=ca-certificates state=latest
when: ansible_os_family == 'RedHat'

View file

@ -6,10 +6,12 @@
- name: Get ansible source dir
sudo: false
shell: "cd ~ && pwd"
shell: "cd ~/ansible && pwd"
register: results
- shell: ". hacking/env-setup && cd test/integration && make {{ run_integration_make_target }}"
- shell: "ls -la && . hacking/env-setup && cd test/integration && make {{ run_integration_make_target }}"
args:
chdir: "{{ results.stdout }}"
async: 3600
poll: 0
register: async_test_results
@ -17,14 +19,13 @@
environment:
TEST_FLAGS: "{{ run_integration_test_flags|default(lookup('env', 'TEST_FLAGS')) }}"
CREDENTIALS_FILE: "{{ run_integration_credentials_file|default(lookup('env', 'CREDENTIALS_FILE')) }}"
args:
chdir: "{{ results.stdout }}/ansible"
- name: poll for test results
async_status:
jid: "{{async_test_results.ansible_job_id}}"
async_status: jid="{{async_test_results.ansible_job_id}}"
register: test_results
until: test_results.finished
retries: 360
delay: 10
retries: 120
delay: 30
ignore_errors: true
- debug: var=test_results