Created
September 3, 2015 20:39
-
-
Save bitle/0a1e2f367a5f6a9e7ebb to your computer and use it in GitHub Desktop.
Loop plugin for Ansible 2 for recursive template lookup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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