Jinja Tricks

difference

{{ [1, 2, 3] | difference([2, 3, 4]) | join(', ') }}
>> 1

avg, min, max, is_list,

{{ [1, 2, 3] | avg }}

generate random UID

{{ 'random' | uuid }}

date format

{{ 1457456400 | date_format }}
{{ 1457456400 | date_format('%d.%m.%Y %H:%M') }}
2017-03-08
08.03.2017 17:00

string to number

{{ '5' | to_num }}

run Salt execution module

{{ salt.cmd.run('whoami') }}
{{ salt.group.add('newgroup1') }}
regex match
{{ 'abcd' | regex_match('BC(.*)', ignorecase=True) }}

regex search

{{ 'muppet baby' | regex_search('pet(.*)', ignorecase=True) }}
>> baby

compare_lists, compare_dicts

{{ [1,2,3] | compare_lists([1,2,4]) }}
>> {'new': 4, 'old': 3}

list files in a directory

{{ '/etc/salt/' | list_files | join('\n') }}

escape Jinja syntax

{% raw %}
    some text that contains jinja {% characters that need to be escaped
{% endraw %}

iterate a dictionary

parent_dict = [{'A':'val1','B':'val2'}]
{% for item in parent_dict %}
  {% for key,val in item.items() %}
     {{ key }} {{ val }}