mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-23 05:10:22 -07:00
cleanup example config file + Squashed commit of the following:
commit c36b66dc952dfff91043ecbca56cf3f1f8f00703 Merge:240d7bf
f4cf934 Author: Michael DeHaan <michael@ansibleworks.com> Date: Tue Jun 18 13:04:51 2013 -0400 Merge branch 'unevaluated-vars' of git://github.com/lorin/ansible into lorin_undefined Conflicts: lib/ansible/runner/__init__.py commit f4cf93436767f73b62a16067ab5e628830045896 Merge: 253144007a1365
Author: Lorin Hochstein <lorin@nimbisservices.com> Date: Thu Jun 6 11:07:41 2013 -0400 Merge branch 'devel' into unevaluated-vars commit 253144045cbafd7d72836f1017c62ac4ba623186 Author: Lorin Hochstein <lorin@nimbisservices.com> Date: Thu Jun 6 11:06:37 2013 -0400 Fail template from file on undefined vars If config option is set, raise an exception if templating from a file and a variable is undefined. commit aecb71d8b75257f0f3e11a9b176fc3737aecef8d Author: Lorin Hochstein <lorin@nimbisservices.com> Date: Wed Jun 5 17:12:12 2013 -0400 Add fail_on_undefined flag Add a fail_on_undefined flag to the template and template_from_string methods. If this flag is true, then re-raise the ninja2.excpetions.UndefinedError instead of swallowing it. commit cbb1808f0585f01536240aee05a1bfd06c4b4647 Merge: d4bbf4941425fb
Author: Lorin Hochstein <lorin@nimbisservices.com> Date: Wed Jun 5 16:14:12 2013 -0400 Merge branch 'devel' into unevaluated-vars commit d4bbf492b0b63c789d66ab60d0ec634d100fca82 Author: Lorin Hochstein <lorin@nimbisservices.com> Date: Mon Jun 3 19:46:13 2013 -0400 template: Raise UndefinedError exception In template_from_string, raise an undefined error if it occurs. Have the caller catch it and throw an AnsibleUndefinedVariable commit c94780280515f1f3756fdc429b2b1e87b365e9b7 Merge: 8d919d6be33bcf
Author: Lorin Hochstein <lorin@nimbisservices.com> Date: Mon Jun 3 10:09:43 2013 -0400 Merge branch 'devel' into unevaluated-vars commit 8d919d6c97b28a42f47ca7248c542695baf6175f Merge: 0f68ad8b8630d2
Author: Lorin Hochstein <lorin@nimbisservices.com> Date: Thu May 30 16:27:48 2013 -0400 Merge branch 'devel' into unevaluated-vars commit 0f68ad8193ac17488e339a258f8c63fdae399c26 Author: Lorin Hochstein <lorin@nimbisservices.com> Date: Thu May 30 14:32:03 2013 -0400 Optionally fail task on undefined variables This patch introduces a new configuration option called error_on_undefined_vars, which defaults to false. If this option is set to true, then a task which has unevaluated variables in its arguments will fail instead of running. Output looks like this: TASK: [set rabbitmq password] ************************************************* fatal: [10.20.0.7] => Undefined variables: rabbitmq_user, rabbitmq_password
This commit is contained in:
parent
240d7bf197
commit
637983cf31
5 changed files with 102 additions and 144 deletions
|
@ -110,6 +110,8 @@ ANSIBLE_NOCOWS = get_config(p, DEFAULTS, 'nocows', 'ANSIBLE_NOCO
|
|||
ANSIBLE_SSH_ARGS = get_config(p, 'ssh_connection', 'ssh_args', 'ANSIBLE_SSH_ARGS', None)
|
||||
ZEROMQ_PORT = int(get_config(p, 'fireball', 'zeromq_port', 'ANSIBLE_ZEROMQ_PORT', 5099))
|
||||
|
||||
DEFAULT_UNDEFINED_VAR_BEHAVIOR = get_config(p, DEFAULTS, 'error_on_undefined_vars', 'ANSIBLE_ERROR_ON_UNDEFINED_VARS', False)
|
||||
|
||||
# non-configurable things
|
||||
DEFAULT_SUDO_PASS = None
|
||||
DEFAULT_REMOTE_PASS = None
|
||||
|
|
|
@ -32,3 +32,6 @@ class AnsibleConnectionFailed(AnsibleError):
|
|||
|
||||
class AnsibleYAMLValidationFailed(AnsibleError):
|
||||
pass
|
||||
|
||||
class AnsibleUndefinedVariable(AnsibleError):
|
||||
pass
|
||||
|
|
|
@ -30,6 +30,7 @@ import base64
|
|||
import sys
|
||||
import shlex
|
||||
import pipes
|
||||
import jinja2
|
||||
|
||||
import ansible.constants as C
|
||||
import ansible.inventory
|
||||
|
@ -128,7 +129,8 @@ class Runner(object):
|
|||
check=False, # don't make any changes, just try to probe for potential changes
|
||||
diff=False, # whether to show diffs for template files that change
|
||||
environment=None, # environment variables (as dict) to use inside the command
|
||||
complex_args=None # structured data in addition to module_args, must be a dict
|
||||
complex_args=None, # structured data in addition to module_args, must be a dict
|
||||
error_on_undefined_vars=C.DEFAULT_UNDEFINED_VAR_BEHAVIOR # ex. False
|
||||
):
|
||||
|
||||
if not complex_args:
|
||||
|
@ -164,6 +166,7 @@ class Runner(object):
|
|||
self.environment = environment
|
||||
self.complex_args = complex_args
|
||||
self.module_with_list = False
|
||||
self.error_on_undefined_vars = error_on_undefined_vars
|
||||
|
||||
self.callbacks.runner = self
|
||||
|
||||
|
@ -446,11 +449,11 @@ class Runner(object):
|
|||
if type(complex_args) != dict:
|
||||
raise errors.AnsibleError("args must be a dictionary, received %s" % complex_args)
|
||||
result = self._executor_internal_inner(
|
||||
host,
|
||||
self.module_name,
|
||||
self.module_args,
|
||||
inject,
|
||||
port,
|
||||
host,
|
||||
self.module_name,
|
||||
self.module_args,
|
||||
inject,
|
||||
port,
|
||||
complex_args=complex_args
|
||||
)
|
||||
results.append(result.result)
|
||||
|
@ -576,8 +579,12 @@ class Runner(object):
|
|||
tmp = self._make_tmp_path(conn)
|
||||
|
||||
# render module_args and complex_args templates
|
||||
module_args = template.template(self.basedir, module_args, inject)
|
||||
complex_args = template.template(self.basedir, complex_args, inject)
|
||||
try:
|
||||
module_args = template.template(self.basedir, module_args, inject, fail_on_undefined=self.error_on_undefined_vars)
|
||||
complex_args = template.template(self.basedir, complex_args, inject, fail_on_undefined=self.error_on_undefined_vars)
|
||||
except jinja2.exceptions.UndefinedError, e:
|
||||
raise errors.AnsibleUndefinedVariable("Undefined variables: %s" % str(e))
|
||||
|
||||
|
||||
result = handler.run(conn, tmp, module_name, module_args, inject, complex_args)
|
||||
|
||||
|
@ -644,7 +651,7 @@ class Runner(object):
|
|||
path = pipes.quote(path)
|
||||
# The following test needs to be SH-compliant. BASH-isms will
|
||||
# not work if /bin/sh points to a non-BASH shell.
|
||||
test = "rc=0; [ -r \"%s\" ] || rc=2; [ -f \"%s\" ] || rc=1; [ -d \"%s\" ] && rc=3" % ((path,) * 3)
|
||||
test = "rc=0; [ -r \"%s\" ] || rc=2; [ -f \"%s\" ] || rc=1; [ -d \"%s\" ] && rc=3" % ((path,) * 3)
|
||||
md5s = [
|
||||
"(/usr/bin/md5sum %s 2>/dev/null)" % path, # Linux
|
||||
"(/sbin/md5sum -q %s 2>/dev/null)" % path, # ?
|
||||
|
|
|
@ -34,7 +34,7 @@ class Globals(object):
|
|||
FILTERS = None
|
||||
|
||||
def __init__(self):
|
||||
pass
|
||||
pass
|
||||
|
||||
def _get_filters():
|
||||
''' return filter plugin instances '''
|
||||
|
@ -48,7 +48,7 @@ def _get_filters():
|
|||
for fp in plugins:
|
||||
filters.update(fp.filters())
|
||||
Globals.FILTERS = filters
|
||||
|
||||
|
||||
return Globals.FILTERS
|
||||
|
||||
def _get_extensions():
|
||||
|
@ -90,7 +90,7 @@ def lookup(name, *args, **kwargs):
|
|||
|
||||
def _legacy_varFindLimitSpace(basedir, vars, space, part, lookup_fatal, depth, expand_lists):
|
||||
''' limits the search space of space to part
|
||||
|
||||
|
||||
basically does space.get(part, None), but with
|
||||
templating for part and a few more things
|
||||
'''
|
||||
|
@ -295,7 +295,7 @@ def legacy_varReplace(basedir, raw, vars, lookup_fatal=True, depth=0, expand_lis
|
|||
|
||||
# TODO: varname is misnamed here
|
||||
|
||||
def template(basedir, varname, vars, lookup_fatal=True, depth=0, expand_lists=True, convert_bare=False):
|
||||
def template(basedir, varname, vars, lookup_fatal=True, depth=0, expand_lists=True, convert_bare=False, fail_on_undefined=False):
|
||||
''' templates a data structure by traversing it and substituting for other data structures '''
|
||||
|
||||
if convert_bare and isinstance(varname, basestring):
|
||||
|
@ -305,7 +305,7 @@ def template(basedir, varname, vars, lookup_fatal=True, depth=0, expand_lists=Tr
|
|||
|
||||
if isinstance(varname, basestring):
|
||||
if '{{' in varname or '{%' in varname:
|
||||
varname = template_from_string(basedir, varname, vars)
|
||||
varname = template_from_string(basedir, varname, vars, fail_on_undefined)
|
||||
if not '$' in varname:
|
||||
return varname
|
||||
|
||||
|
@ -341,7 +341,7 @@ class _jinja2_vars(object):
|
|||
is avoiding duplicating the large hashes that inject tends to be.
|
||||
To facilitate using builtin jinja2 things like range, globals are handled
|
||||
here.
|
||||
extras is a list of locals to also search for variables.
|
||||
extras is a list of locals to also search for variables.
|
||||
'''
|
||||
|
||||
def __init__(self, basedir, vars, globals, *extras):
|
||||
|
@ -398,6 +398,8 @@ class J2Template(jinja2.environment.Template):
|
|||
def template_from_file(basedir, path, vars):
|
||||
''' run a file through the templating engine '''
|
||||
|
||||
fail_on_undefined = C.DEFAULT_UNDEFINED_VAR_BEHAVIOR
|
||||
|
||||
from ansible import utils
|
||||
realpath = utils.path_dwim(basedir, path)
|
||||
loader=jinja2.FileSystemLoader([basedir,os.path.dirname(realpath)])
|
||||
|
@ -409,6 +411,8 @@ def template_from_file(basedir, path, vars):
|
|||
environment = jinja2.Environment(loader=loader, trim_blocks=True, extensions=_get_extensions())
|
||||
environment.filters.update(_get_filters())
|
||||
environment.globals['lookup'] = my_lookup
|
||||
if fail_on_undefined:
|
||||
environment.undefined = StrictUndefined
|
||||
|
||||
try:
|
||||
data = codecs.open(realpath, encoding="utf8").read()
|
||||
|
@ -417,7 +421,7 @@ def template_from_file(basedir, path, vars):
|
|||
except:
|
||||
raise errors.AnsibleError("unable to read %s" % realpath)
|
||||
|
||||
|
||||
|
||||
# Get jinja env overrides from template
|
||||
if data.startswith(JINJA2_OVERRIDE):
|
||||
eol = data.find('\n')
|
||||
|
@ -455,15 +459,18 @@ def template_from_file(basedir, path, vars):
|
|||
# This line performs deep Jinja2 magic that uses the _jinja2_vars object for vars
|
||||
# Ideally, this could use some API where setting shared=True and the object won't get
|
||||
# passed through dict(o), but I have not found that yet.
|
||||
res = jinja2.utils.concat(t.root_render_func(t.new_context(_jinja2_vars(basedir, vars, t.globals), shared=True)))
|
||||
try:
|
||||
res = jinja2.utils.concat(t.root_render_func(t.new_context(_jinja2_vars(basedir, vars, t.globals), shared=True)))
|
||||
except jinja2.exceptions.UndefinedError, e:
|
||||
raise errors.AnsibleUndefinedVariable("Undefined variables: %s" % str(e))
|
||||
|
||||
if data.endswith('\n') and not res.endswith('\n'):
|
||||
res = res + '\n'
|
||||
return template(basedir, res, vars)
|
||||
|
||||
def template_from_string(basedir, data, vars):
|
||||
def template_from_string(basedir, data, vars, fail_on_undefined=False):
|
||||
''' run a string through the (Jinja2) templating engine '''
|
||||
|
||||
|
||||
try:
|
||||
if type(data) == str:
|
||||
data = unicode(data, 'utf-8')
|
||||
|
@ -487,16 +494,19 @@ def template_from_string(basedir, data, vars):
|
|||
raise errors.AnsibleError("recursive loop detected in template string: %s" % data)
|
||||
else:
|
||||
return data
|
||||
|
||||
|
||||
def my_lookup(*args, **kwargs):
|
||||
kwargs['vars'] = vars
|
||||
return lookup(*args, basedir=basedir, **kwargs)
|
||||
|
||||
|
||||
t.globals['lookup'] = my_lookup
|
||||
|
||||
|
||||
res = jinja2.utils.concat(t.root_render_func(t.new_context(_jinja2_vars(basedir, vars, t.globals), shared=True)))
|
||||
return res
|
||||
except jinja2.exceptions.UndefinedError:
|
||||
if fail_on_undefined:
|
||||
raise
|
||||
else:
|
||||
# this shouldn't happen due to undeclared check above
|
||||
return data
|
||||
return data
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue