ansible.template package

class ansible.template.Templar(loader, shared_loader_obj=None, variables={})[source]

Bases: object

The main class for templating, with the main entry-point of template().

set_available_variables(variables)[source]

Sets the list of template variables this Templar instance will use to template things, so we don’t have to pass them around between internal methods. We also clear the template cache here, as the variables are being changed.

template(variable, convert_bare=False, preserve_trailing_newlines=True, escape_backslashes=True, fail_on_undefined=None, overrides=None, convert_data=True, static_vars=[''], cache=True, bare_deprecated=True)[source]

Templates (possibly recursively) any given data as input. If convert_bare is set to True, the given data will be wrapped as a jinja2 variable (‘{{foo}}’) before being sent through the template engine.

Submodules

ansible.template.safe_eval module

ansible.template.safe_eval.safe_eval(expr, locals={}, include_exceptions=False)[source]

This is intended for allowing things like: with_items: a_list_variable

Where Jinja2 would return a string but we do not want to allow it to call functions (outside of Jinja2, where the env is constrained). If the input data to this function came from an untrusted (remote) source, it should first be run through _clean_data_struct() to ensure the data is further sanitized prior to evaluation.

Based on: http://stackoverflow.com/questions/12523516/using-ast-and-whitelists-to-make-pythons-eval-safe

ansible.template.template module

class ansible.template.template.AnsibleJ2Template[source]

Bases: jinja2.environment.Template

A helper class, which prevents Jinja2 from running _jinja2_vars through dict(). Without this, {% include %} and similar will create new contexts unlike the special one created in template_from_file. This ensures they are all alike, except for potential locals.

new_context(vars=None, shared=False, locals=None)[source]

ansible.template.vars module

class ansible.template.vars.AnsibleJ2Vars(templar, globals, locals=None, *extras)[source]

Bases: object

Helper class to template all variable content before jinja2 sees it. This is done by hijacking the variable storage that jinja2 uses, and overriding __contains__ and __getitem__ to look like a dict. Added bonus is avoiding duplicating the large hashes that inject tends to be.

To facilitate using builtin jinja2 things like range, globals are also handled here.

add_locals(locals)[source]

If locals are provided, create a copy of self containing those locals in addition to what is already in this variable proxy.