Chapter 6: Unlocking the Power of Jinja2 Templates

Manipulating configuration files by hand is a tedious and error-prone task. Equally, performing pattern matching to make changes to existing files is risky, and ensuring that the patterns are reliable and accurate can be time-consuming. Whether you are using Ansible to define configuration file content, perform variable substitution in tasks, evaluate conditional statements, or beyond, templating comes into play with nearly every Ansible playbook. In fact, given the importance of this task, it could be said that templating is the lifeblood of Ansible.

The templating engine employed by Ansible is Jinja2, which is a modern and designer-friendly templating language for Python. Jinja2 deserves its ...

Get Mastering Ansible - Fourth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.