Skip to content

Instantly share code, notes, and snippets.

@DazWorrall
Last active December 17, 2015 08:59
Show Gist options
  • Save DazWorrall/5584358 to your computer and use it in GitHub Desktop.
Save DazWorrall/5584358 to your computer and use it in GitHub Desktop.
Different value of `inject` in ansible lookup plugins. In the first case, `inject` is a dictionary with host/group/fact data, in the second case, its a function.
# Place in lookup_plugins/foo/mytest.py
from ansible import utils
class LookupModule(object):
def __init__(self, basedir=None, **kwargs):
self.basedir = basedir
def run(self, terms, inject=None, **kwargs):
terms = utils.listify_lookup_plugin_terms(terms, self.basedir, inject)
ret = []
if not isinstance(terms, list):
terms = [ terms ]
for term in terms:
ret.append(repr(inject)) # This is what we're interested in
return ret
$ ansible-playbook -c local -i localhost, test.yml
PLAY [all] ********************************************************************
GATHERING FACTS ***************************************************************
ok: [localhost]
TASK: [debug msg={{item}}] ****************************************************
ok: [localhost] => (item={'delegate_to': None, u'ansible_en1': ....<snipped, there is a bunch of fact/host/group data here>'})
TASK: [debug msg=<built-in function vars>] ************************************
ok: [localhost]
PLAY RECAP ********************************************************************
localhost : ok=3 changed=0 unreachable=0 failed=0
---
- hosts: all
tasks:
- debug: msg={{ item }}
with_mytest: foo
- debug: msg={{ lookup('mytest','foo') }}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment