when defined not working with hash

31 views
Skip to first unread message

jepper

unread,
Dec 16, 2015, 6:35:15 AM12/16/15
to Ansible Project
I need to do something like this in a role, and I use an endpoint for some of the items in the hash I feed it

  shell: dosomething.sh {{ item.endpoint }}
  when: "{{ item.endpoint }} is defined"
  with_items: {{ myhash }}

myhash:
  - { version: '{{ cqrs_version }}' }
  - { version: '{{ staging_version }}', endpoint: 'http://localhost:8080/staging' }

It breaks as follows:

TASK: [gavdeploy | wait for endpoints to become available] ********************
fatal: [ststxdapbe02] => Traceback (most recent call last):
  File "/usr/local/Cellar/ansible/1.9.3/libexec/lib/python2.7/site-packages/ansible/runner/__init__.py", line 586, in _executor
    exec_rc = self._executor_internal(host, new_stdin)
  File "/usr/local/Cellar/ansible/1.9.3/libexec/lib/python2.7/site-packages/ansible/runner/__init__.py", line 815, in _executor_internal
    complex_args=complex_args
  File "/usr/local/Cellar/ansible/1.9.3/libexec/lib/python2.7/site-packages/ansible/runner/__init__.py", line 869, in _executor_internal_inner
    if not utils.check_conditional(cond, self.basedir, inject, fail_on_undefined=self.error_on_undefined_vars):
  File "/usr/local/Cellar/ansible/1.9.3/libexec/lib/python2.7/site-packages/ansible/utils/__init__.py", line 265, in check_conditional
    conditional = template.template(basedir, conditional, inject, fail_on_undefined=fail_on_undefined)
  File "/usr/local/Cellar/ansible/1.9.3/libexec/lib/python2.7/site-packages/ansible/utils/template.py", line 122, in template
    varname = template_from_string(basedir, varname, templatevars, fail_on_undefined)
  File "/usr/local/Cellar/ansible/1.9.3/libexec/lib/python2.7/site-packages/ansible/utils/template.py", line 371, in template_from_string
    res = jinja2.utils.concat(rf)
  File "<template>", line 8, in root
  File "/usr/local/Cellar/ansible/1.9.3/libexec/vendor/lib/python2.7/site-packages/jinja2/runtime.py", line 489, in _fail_with_undefined_error
    raise self._undefined_exception(hint)
UndefinedError: 'dict object' has no attribute 'endpoint'

FATAL: all hosts have already failed -- aborting

jepper

unread,
Dec 16, 2015, 9:01:04 AM12/16/15
to Ansible Project
figured it out. Simply

when: "item.endpoint is defined"
Reply all
Reply to author
Forward
0 new messages