Skip to content

Instantly share code, notes, and snippets.

@bitle
Created September 3, 2015 20:39
Show Gist options
  • Save bitle/0a1e2f367a5f6a9e7ebb to your computer and use it in GitHub Desktop.
Save bitle/0a1e2f367a5f6a9e7ebb to your computer and use it in GitHub Desktop.
Loop plugin for Ansible 2 for recursive template lookup
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
import os
import glob
from ansible.plugins.lookup import LookupBase
class LookupModule(LookupBase):
def run(self, terms, variables=None, **kwargs):
basedir = self.get_basedir(variables)
ret = []
for term in terms:
term_file = os.path.basename(term)
dwimmed_path = self._loader.path_dwim_relative(basedir, 'templates', os.path.dirname(term))
globbed = [y for x in os.walk(dwimmed_path) for y in glob.glob(os.path.join(x[0], term_file))]
ret.extend(os.path.relpath(g, dwimmed_path) for g in globbed if os.path.isfile(g))
return ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment