From the course: Ansible: From Basics to Guru by Pearson

Unlock this course with a free trial

Join today to access over 25,200 courses taught by industry experts.

Using conditional statements in templates

Using conditional statements in templates - Ansible Tutorial

From the course: Ansible: From Basics to Guru by Pearson

Using conditional statements in templates

Templates are becoming really powerful if you apply conditionals to them. So how does that work? Well, in Jinja2, variables and logic expressions are placed between delimiters. And to evaluate a variable, it is placed between double curly braces. That's what we do with ordinary variables in all of the Ansible playbooks. And that's because variables are interpreted using Jinja too. Logical expressions are written as curly brace percent expression and then a percent and a curly brace. And they can be used for control structures or loops. As in for file in my files, which is evaluating the content of a variable myFiles. Or for user in myUsers if not user is root. That's a little bit more of a complex control structure. Control structures in Jinja2 are really very powerful and I'm not giving you a complete overview of everything that can be done, but I want to have a look at one of the examples that is clearly documented in the Ansible documentation. Let's check it out. So in the Ansible…

Contents