Commit 7eedc3fb by Michael DeHaan

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.
parent 3ee22ad3
...@@ -25,7 +25,8 @@ import shlex ...@@ -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,6 +83,33 @@ class PlayBook(object): ...@@ -82,6 +83,33 @@ class PlayBook(object):
self.playbook = self._parse_playbook(playbook) self.playbook = self._parse_playbook(playbook)
def _include_tasks(self, play, task, dirname, new_tasks):
# an include line looks like:
# include: some.yml a=2 b=3 c=4
include_tokens = task['include'].split()
path = path_dwim(dirname, include_tokens[0])
inject_vars = play.get('vars', {})
for i,x in enumerate(include_tokens):
if x.find("=") != -1:
(k,v) = x.split("=")
inject_vars[k] = v
included = file(path).read()
template = jinja2.Template(included)
included = template.render(inject_vars)
included = yaml.load(included)
for x in included:
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): def _parse_playbook(self, playbook):
''' load YAML file, including handling for imported files ''' ''' load YAML file, including handling for imported files '''
...@@ -96,22 +124,7 @@ class PlayBook(object): ...@@ -96,22 +124,7 @@ class PlayBook(object):
new_tasks = [] new_tasks = []
for task in tasks: for task in tasks:
if 'include' in task: if 'include' in task:
# FIXME: refactor self._include_tasks(play, task, dirname, new_tasks)
# an include line looks like:
# include: some.yml a=2 b=3 c=4
include_tokens = task['include'].split()
path = path_dwim(dirname, include_tokens[0])
inject_vars = play.get('vars', {})
for i,x in enumerate(include_tokens):
if x.find("=") != -1:
(k,v) = x.split("=")
inject_vars[k] = v
included = file(path).read()
template = jinja2.Template(included)
included = template.render(inject_vars)
included = yaml.load(included)
for x in included:
new_tasks.append(x)
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): ...@@ -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
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment