Breakout includes into seperate functions, allow vars to apply to handlers but handlers

still may not be parameterized because it does not make sense to import them more than
once since they are keyed by name.
pull/3/head
Michael DeHaan 12 years ago
parent 3ee22ad351
commit 7eedc3fb1a

@ -25,7 +25,8 @@ import shlex
import os import os
import jinja2 import jinja2
SETUP_CACHE={ 'foo' : {} } # used to transfer variables to Runner
SETUP_CACHE={ }
############################################# #############################################
@ -82,21 +83,7 @@ class PlayBook(object):
self.playbook = self._parse_playbook(playbook) self.playbook = self._parse_playbook(playbook)
def _parse_playbook(self, playbook): def _include_tasks(self, play, task, dirname, new_tasks):
''' load YAML file, including handling for imported files '''
dirname = os.path.dirname(playbook)
playbook = yaml.load(file(playbook).read())
for play in playbook:
tasks = play.get('tasks',[])
handlers = play.get('handlers', [])
# process tasks in this file as well as imported tasks
new_tasks = []
for task in tasks:
if 'include' in task:
# FIXME: refactor
# an include line looks like: # an include line looks like:
# include: some.yml a=2 b=3 c=4 # include: some.yml a=2 b=3 c=4
include_tokens = task['include'].split() include_tokens = task['include'].split()
@ -112,6 +99,32 @@ class PlayBook(object):
included = yaml.load(included) included = yaml.load(included)
for x in included: for x in included:
new_tasks.append(x) new_tasks.append(x)
def _include_handlers(self, play, handler, dirname, new_handlers):
path = path_dwim(dirname, handler['include'])
included = file(path).read()
inject_vars = play.get('vars', {})
template = jinja2.Template(included)
included = template.render(inject_vars)
included = yaml.load(included)
for x in included:
new_handlers.append(x)
def _parse_playbook(self, playbook):
''' load YAML file, including handling for imported files '''
dirname = os.path.dirname(playbook)
playbook = yaml.load(file(playbook).read())
for play in playbook:
tasks = play.get('tasks',[])
handlers = play.get('handlers', [])
# process tasks in this file as well as imported tasks
new_tasks = []
for task in tasks:
if 'include' in task:
self._include_tasks(play, task, dirname, new_tasks)
else: else:
new_tasks.append(task) new_tasks.append(task)
play['tasks'] = new_tasks play['tasks'] = new_tasks
@ -120,10 +133,7 @@ class PlayBook(object):
new_handlers = [] new_handlers = []
for handler in handlers: for handler in handlers:
if 'include' in handler: if 'include' in handler:
path = path_dwim(dirname, handler['include']) self._include_handlers(play, handler, dirname, new_handlers)
included = yaml.load(file(path).read())
for x in included:
new_handlers.append(x)
else: else:
new_handlers.append(handler) new_handlers.append(handler)
play['handlers'] = new_handlers play['handlers'] = new_handlers

Loading…
Cancel
Save