Merge pull request #1766 from dhozac/complex-vars-in-playbook-include

Allow parameterized playbook includes to define complex variables
pull/1782/head
Michael DeHaan 12 years ago
commit 89dab0b12c

@ -138,40 +138,45 @@ class PlayBook(object):
if type(play) != dict: if type(play) != dict:
raise errors.AnsibleError("parse error: each play in a playbook must a YAML dictionary (hash), recieved: %s" % play) raise errors.AnsibleError("parse error: each play in a playbook must a YAML dictionary (hash), recieved: %s" % play)
if 'include' in play: if 'include' in play:
if len(play.keys()) <= 2: tokens = shlex.split(play['include'])
tokens = shlex.split(play['include'])
items = [''] items = ['']
for k in play.keys(): for k in play.keys():
if not k.startswith("with_"): if not k.startswith("with_"):
# These are the keys allowed to be mixed with playbook includes
if k in ("include", "vars"):
continue continue
plugin_name = k[5:] else:
if plugin_name not in utils.plugins.lookup_loader: raise errors.AnsibleError("parse error: playbook includes cannot be used with other directives: %s" % play)
raise errors.AnsibleError("cannot find lookup plugin named %s for usage in with_%s" % (plugin_name, plugin_name)) plugin_name = k[5:]
terms = utils.template_ds(basedir, play[k], vars) if plugin_name not in utils.plugins.lookup_loader:
items = utils.plugins.lookup_loader.get(plugin_name, basedir=basedir, runner=None).run(terms, inject=vars) raise errors.AnsibleError("cannot find lookup plugin named %s for usage in with_%s" % (plugin_name, plugin_name))
break terms = utils.template_ds(basedir, play[k], vars)
items = utils.plugins.lookup_loader.get(plugin_name, basedir=basedir, runner=None).run(terms, inject=vars)
for item in items:
incvars = vars.copy() for item in items:
incvars['item'] = item incvars = vars.copy()
for t in tokens[1:]: incvars['item'] = item
(k,v) = t.split("=", 1) if 'vars' in play:
incvars[k] = utils.template_ds(basedir, v, incvars) if isinstance(play['vars'], dict):
included_path = utils.path_dwim(basedir, tokens[0]) incvars.update(play['vars'])
(plays, basedirs) = self._load_playbook_from_file(included_path, incvars) elif isinstance(play['vars'], list):
for p in plays: for v in play['vars']:
if 'vars' not in p: incvars.update(v)
p['vars'] = {} for t in tokens[1:]:
if isinstance(p['vars'], dict): (k,v) = t.split("=", 1)
p['vars'].update(incvars) incvars[k] = utils.template_ds(basedir, v, incvars)
elif isinstance(p['vars'], list): included_path = utils.path_dwim(basedir, tokens[0])
p['vars'].extend([dict(k=v) for k,v in incvars.iteritems()]) (plays, basedirs) = self._load_playbook_from_file(included_path, incvars)
accumulated_plays.extend(plays) for p in plays:
play_basedirs.extend(basedirs) if 'vars' not in p:
p['vars'] = {}
else: if isinstance(p['vars'], dict):
raise errors.AnsibleError("parse error: playbook includes cannot be used with other directives: %s" % play) p['vars'].update(incvars)
elif isinstance(p['vars'], list):
p['vars'].extend([dict(k=v) for k,v in incvars.iteritems()])
accumulated_plays.extend(plays)
play_basedirs.extend(basedirs)
else: else:
accumulated_plays.append(play) accumulated_plays.append(play)
play_basedirs.append(basedir) play_basedirs.append(basedir)

Loading…
Cancel
Save