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" %}
metadata_endpoint: http://169.254.169.254/latest/meta-data/
{% endif plat == "digitalocean" %}  
metadata_endpoint: http://169.254.169.254/metadata/v1/
{% 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: http://169.254.169.254/latest/meta-data/.

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.