parameterized first found

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 “”, 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 ?

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]
}}

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.

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.

fixed in https://github.com/ansible/ansible/pull/14281, will be out in the next releases