parameterized first found

139 views
Skip to first unread message

vadimv Vatlin

unread,
Jan 14, 2016, 1:17:46 PM1/14/16
to Ansible Project
This code works with ansible 1.9.4

- name: Configure user parameters
  template: src={{ lookup('first_found', [ 'userparameter_' + item + '_' + ansible_pkg_mgr + '.j2', 'userparameter_' + item + '.j2' ]) }} dest=/etc/zabbix/zabbix_agentd.d/userparameter_{{ item }}.conf
  with_items: "{{ zabbixagent_userparameters }}"
  when: zabbixagent_userparameters is defined
  notify:
    - restart zabbix-agent

But not working with 2.0.0.1

TASK [zabbix-agent : Configure user parameters] ********************************
task path: /etc/ansible/roles/zabbix-agent/tasks/main.yml:23
An exception occurred during task execution. The full traceback is:
Traceback (most recent call last):
  File "/usr/lib/pymodules/python2.7/ansible/executor/process/worker.py", line 114, in run
    self._shared_loader_obj,
  File "/usr/lib/pymodules/python2.7/ansible/executor/task_executor.py", line 91, in run
    item_results = self._run_loop(items)
  File "/usr/lib/pymodules/python2.7/ansible/executor/task_executor.py", line 241, in _run_loop
    res = self._execute(variables=task_vars)
  File "/usr/lib/pymodules/python2.7/ansible/executor/task_executor.py", line 356, in _execute
    self._task.post_validate(templar=templar)
  File "/usr/lib/pymodules/python2.7/ansible/playbook/task.py", line 238, in post_validate
    super(Task, self).post_validate(templar)
  File "/usr/lib/pymodules/python2.7/ansible/playbook/base.py", line 307, in post_validate
    value = templar.template(getattr(self, name))
  File "/usr/lib/pymodules/python2.7/ansible/template/__init__.py", line 350, in template
    d[k] = self.template(variable[k], preserve_trailing_newlines=preserve_trailing_newlines, fail_on_undefined=fail_on_undefined, overrides=overrides)
  File "/usr/lib/pymodules/python2.7/ansible/template/__init__.py", line 322, in template
    result = self._do_template(variable, preserve_trailing_newlines=preserve_trailing_newlines, escape_backslashes=escape_backslashes, fail_on_undefined=fail_on_undefined, overrides=overrides)
  File "/usr/lib/pymodules/python2.7/ansible/template/__init__.py", line 474, in _do_template
    res = j2_concat(rf)
  File "<template>", line 10, in root
  File "/usr/lib/python2.7/dist-packages/jinja2/runtime.py", line 193, in call
    return __obj(*args, **kwargs)
  File "/usr/lib/pymodules/python2.7/ansible/template/__init__.py", line 401, in _lookup
    ran = instance.run(loop_terms, variables=self._available_variables, **kwargs)
  File "/usr/lib/pymodules/python2.7/ansible/plugins/lookup/first_found.py", line 183, in run
    if os.path.isabs(fn) and os.path.exists(fn):
  File "/usr/lib/python2.7/posixpath.py", line 61, in isabs
    return s.startswith('/')
AttributeError: 'list' object has no attribute 'startswith'

What I do wrong ?  

Brian Coca

unread,
Jan 28, 2016, 7:48:58 PM1/28/16
to Ansible Project
the lookup is returning a list, not a string, as a workaround:

template: src={{ lookup('first_found', [ 'userparameter_' + item +
'_' + ansible_pkg_mgr + '.j2', 'userparameter_' + item + '.j2' ])[0]
}}


--
Brian Coca

vadimv Vatlin

unread,
Jan 29, 2016, 2:08:10 AM1/29/16
to Ansible Project
thank you very much. 

is it make any sense that first_found return a list ?
i think that _first_ means that list will always contain 1 element.

Brian Coca

unread,
Jan 29, 2016, 9:25:27 AM1/29/16
to Ansible Project
looking into that, for most lookups the answer is 'yes', in this case
it can be considered a bug, you should open an issue in github.



--
Brian Coca

Brian Coca

unread,
Feb 3, 2016, 2:58:38 PM2/3/16
to Ansible Project
fixed in https://github.com/ansible/ansible/pull/14281, will be out in the next releases
Reply all
Reply to author
Forward
0 new messages