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

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

Allow parameterized playbook includes to define complex variables
This commit is contained in:
Michael DeHaan 2012-12-16 10:59:53 -08:00
commit 89dab0b12c

View file

@ -138,23 +138,31 @@ 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
else:
raise errors.AnsibleError("parse error: playbook includes cannot be used with other directives: %s" % play)
plugin_name = k[5:] plugin_name = k[5:]
if plugin_name not in utils.plugins.lookup_loader: 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)) raise errors.AnsibleError("cannot find lookup plugin named %s for usage in with_%s" % (plugin_name, plugin_name))
terms = utils.template_ds(basedir, play[k], vars) terms = utils.template_ds(basedir, play[k], vars)
items = utils.plugins.lookup_loader.get(plugin_name, basedir=basedir, runner=None).run(terms, inject=vars) items = utils.plugins.lookup_loader.get(plugin_name, basedir=basedir, runner=None).run(terms, inject=vars)
break
for item in items: for item in items:
incvars = vars.copy() incvars = vars.copy()
incvars['item'] = item incvars['item'] = item
if 'vars' in play:
if isinstance(play['vars'], dict):
incvars.update(play['vars'])
elif isinstance(play['vars'], list):
for v in play['vars']:
incvars.update(v)
for t in tokens[1:]: for t in tokens[1:]:
(k,v) = t.split("=", 1) (k,v) = t.split("=", 1)
incvars[k] = utils.template_ds(basedir, v, incvars) incvars[k] = utils.template_ds(basedir, v, incvars)
@ -169,9 +177,6 @@ class PlayBook(object):
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:
raise errors.AnsibleError("parse error: playbook includes cannot be used with other directives: %s" % play)
else: else:
accumulated_plays.append(play) accumulated_plays.append(play)
play_basedirs.append(basedir) play_basedirs.append(basedir)