1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Playbook + with_items was an undocumented feature that was causing problems because it would

evaluate and replace '$item' with ''.  Really it doesn't make sense to include multiple playbooks
via a loop variable, as you can do this with task + with_items already (and it's a simpler code
path).  Given this is undocumented, this removes that feature, and we'll consider next how to
also add 'with_items' support directly to roles.
This commit is contained in:
Michael DeHaan 2013-04-09 20:13:55 -04:00
parent c64f19dc5f
commit 290780d13f

View file

@ -153,46 +153,45 @@ class PlayBook(object):
for play in playbook_data: for play in playbook_data:
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:
# a playbook (list of plays) decided to include some other list of plays
# from another file. The result is a flat list of plays in the end.
tokens = shlex.split(play['include']) tokens = shlex.split(play['include'])
items = [''] incvars = vars.copy()
for k in play.keys(): if 'vars' in play:
if not k.startswith("with_"): if isinstance(play['vars'], dict):
# These are the keys allowed to be mixed with playbook includes incvars.update(play['vars'])
if k in ("include", "vars"): elif isinstance(play['vars'], list):
continue for v in play['vars']:
else: incvars.update(v)
raise errors.AnsibleError("parse error: playbook includes cannot be used with other directives: %s" % play)
plugin_name = k[5:]
if plugin_name not in utils.plugins.lookup_loader:
raise errors.AnsibleError("cannot find lookup plugin named %s for usage in with_%s" % (plugin_name, plugin_name))
terms = utils.template(basedir, play[k], vars)
items = utils.plugins.lookup_loader.get(plugin_name, basedir=basedir, runner=None).run(terms, inject=vars)
for item in items: # allow key=value parameters to be specified on the include line
incvars = vars.copy() # to set variables
if 'vars' in play:
if isinstance(play['vars'], dict): for t in tokens[1:]:
incvars.update(play['vars'])
elif isinstance(play['vars'], list): (k,v) = t.split("=", 1)
for v in play['vars']: incvars[k] = utils.template(basedir, v, incvars)
incvars.update(v)
for t in tokens[1:]:
(k,v) = t.split("=", 1)
incvars[k] = utils.template(basedir, v, incvars)
included_path = utils.path_dwim(basedir, utils.template(basedir, tokens[0], incvars)) included_path = utils.path_dwim(basedir, utils.template(basedir, tokens[0], incvars))
(plays, basedirs) = self._load_playbook_from_file(included_path, incvars) (plays, basedirs) = self._load_playbook_from_file(included_path, incvars)
for p in plays: for p in plays:
# support for parameterized play includes works by passing
# those variables along to the subservient play
if 'vars' not in p: if 'vars' not in p:
p['vars'] = {} p['vars'] = {}
if isinstance(p['vars'], dict): if isinstance(p['vars'], dict):
p['vars'].update(incvars) p['vars'].update(incvars)
elif isinstance(p['vars'], list): elif isinstance(p['vars'], list):
# nobody should really do this, but handle vars: a=1 b=2
p['vars'].extend([dict(k=v) for k,v in incvars.iteritems()]) p['vars'].extend([dict(k=v) for k,v in incvars.iteritems()])
accumulated_plays.extend(plays) accumulated_plays.extend(plays)
play_basedirs.extend(basedirs) play_basedirs.extend(basedirs)
else: else:
# this is a normal (non-included play)
accumulated_plays.append(play) accumulated_plays.append(play)
play_basedirs.append(basedir) play_basedirs.append(basedir)