Jinja Hydration in Python

Posted by mwguy on Wed 04 March 2020

Jinja is an excellent tool and it's incredibly great at allowing you to make rich and featureful configuration files. Essentially, you can make your configs dynamic. For example if you had an yaml file that looks like this :

{% if plat == "aws" %}
{% endif plat == "digitalocean" %}
{% else %}
metadata_endpoint: http://onprem.local/metadata/hotness/
{% endif %}
You could use the above to give you the "proper" result based on the environment
you're in. So if you wanted to render this bit you could do the following:
jinja_args = {"plat" : "aws"}

this_template = jinja2.Template(var_with_above)

rendered_config = this_template.redner(**jinja_args)

The outcome in rendered_config should include metadata_endpoint:

Hope that helps.

tags: jinja, Python