Allow parameterized playbook includes to define complex variables

To be used like:
- include: playbook.yml
  vars:
    mylistvar:
    - item1
    - item2
This commit is contained in:
Daniel Hokka Zakrisson 2012-12-13 15:05:02 +01:00
parent 6350dedd7a
commit c2792a936d

View file

@ -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: for item in items:
incvars = vars.copy() incvars = vars.copy()
incvars['item'] = item incvars['item'] = item
for t in tokens[1:]: if 'vars' in play:
(k,v) = t.split("=", 1) if isinstance(play['vars'], dict):
incvars[k] = utils.template_ds(basedir, v, incvars) incvars.update(play['vars'])
included_path = utils.path_dwim(basedir, tokens[0]) elif isinstance(play['vars'], list):
(plays, basedirs) = self._load_playbook_from_file(included_path, incvars) for v in play['vars']:
for p in plays: incvars.update(v)
if 'vars' not in p: for t in tokens[1:]:
p['vars'] = {} (k,v) = t.split("=", 1)
if isinstance(p['vars'], dict): incvars[k] = utils.template_ds(basedir, v, incvars)
p['vars'].update(incvars) included_path = utils.path_dwim(basedir, tokens[0])
elif isinstance(p['vars'], list): (plays, basedirs) = self._load_playbook_from_file(included_path, incvars)
p['vars'].extend([dict(k=v) for k,v in incvars.iteritems()]) for p in plays:
accumulated_plays.extend(plays) if 'vars' not in p:
play_basedirs.extend(basedirs) p['vars'] = {}
if isinstance(p['vars'], dict):
else: p['vars'].update(incvars)
raise errors.AnsibleError("parse error: playbook includes cannot be used with other directives: %s" % play) 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)