Using Jinja2 templates with Ansible
Jinja2 is a powerful templating engine for Python and is supported natively by Ansible. It can also be used to generate any text-based files, such as HTML, CSV, or YAML. In this recipe, you will utilize Jinja2 with Ansible variables in order to generate custom CLI configuration files for network devices. It is important to become familiar with Jinja2 as it has become the de facto templating language for network automation.
Note
Jinja2 is a robust templating language that is heavily used in Ansible; however, this book doesn’t intend to teach Jinja2 in deep detail. For more information on Jinja2, visit the documentation: https://jinja.palletsprojects.com/en/stable/.
In the Using Ansible loops section, we defined the ntp variable. Now, let’s use this data and create a configuration snippet for NTP configuration for two Arista cEOS devices.
- Create a templates/ directory that will contain a template...