Hi,
I wrote a lookup plugin to get some data from the already-existing ansible variables, so it needs access to “groups” and “hostvars” to find the right combination of data to return. In particular, my lookup plugin returns a server name, which I want to use in a delegated task, so I have something like the following:
`
- name: Do something on another host
shell: …
delegate_to: “{{ lookup(‘my_lookup’, item) }}”
with_items: some_list
`
That was my first attempt, I then realized I could use my lookup in the ‘with’ clause (as “with_my_lookup”) but got the same result.
The task fails with the following trace:
`
Traceback (most recent call last):
File “/usr/bin/ansible-playbook”, line 86, in
sys.exit(cli.run())
File “/usr/lib/python2.6/site-packages/ansible/cli/playbook.py”, line 150, in run
results = pbex.run()
File “/usr/lib/python2.6/site-packages/ansible/executor/playbook_executor.py”, line 140, in run
result = self._tqm.run(play=play)
File “/usr/lib/python2.6/site-packages/ansible/executor/task_queue_manager.py”, line 238, in run
play_return = strategy.run(iterator, play_context)
File “/usr/lib/python2.6/site-packages/ansible/plugins/strategy/linear.py”, line 229, in run
task_vars = self._variable_manager.get_vars(loader=self._loader, play=iterator._play, host=host, task=task)
File “/usr/lib/python2.6/site-packages/ansible/vars/init.py”, line 341, in get_vars
all_vars[‘ansible_delegated_vars’] = self._get_delegated_vars(loader, play, task, all_vars)
File “/usr/lib/python2.6/site-packages/ansible/vars/init.py”, line 417, in _get_delegated_vars
items = lookup_loader.get(task.loop, loader=loader, templar=templar).run(terms=loop_terms, variables=vars_copy)
File “/project/ansible/lookup_plugins/my_lookup.py”, line 11, in run
if ip in variables[‘hostvars’][server][‘ansible_all_ipv4_addresses’]:
KeyError: ‘hostvars’
That looks like it is running the task while getting the variables, so I’m a little confused. I have the feeling my run() method is not properly implemented and it is causing this to fail. Any help?
`