We have seen in the previous sections that it is always wise to check whether a variable is defined before using it. We can set a default value for the variable so that, instead of failing, Ansible will use that value if the variable is not defined. To do so, we use this:
{{ backup_disk | default("/dev/sdf") }}
This filter will not assign the default value to the variable; it will only pass the default value to the current task where it is being used. Let's look at a few more examples of Jinja filters themselves before closing this section:
- Execute this to get a random character from a list:
{{ ['a', 'b', 'c', 'd'] | random }}
- Execute this to get a random number from 0 to 100:
{{ 100 | random }}
- Execute this to get a random number from 10 to 50:
{{ 50 | random(10) }}
- Execute this to...