Jinja Hydration in Python

By | March 4, 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.

One thought on “Jinja Hydration in Python

  1. Pingback: Hydrating Jinja with JSON as a Middle Step – Mid West Guy

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.