Two main types of Jinja2 syntaxes used in Salt:
Variables
{{ foo }}
{{ foo.bar }}
{{ foo['bar'] }}
{{ get_data() }}
Basic control statements
{% if myvar == 'foo' %}
somecontent
{% elif myvar == 'bar' %}
othercontent
{% else %}
morecontent
{% endif %}
{% for user in ['larry', 'moe', 'curly'] %}
It's user {{ user }}!
Hello {{ user }}!
{% endfor %}
We can also set variables
{% set myvar = 'foo' %}
Jinja2 templates allow us to produce states that are platform agnostic by dynamically choosing the correct content for our state files.
Making use of Grains, we can retrieve the OS type that we are dealing with. With the grains plus the Jinja template, we can specify that if an OS is from a certain type, install X, if it's other type, install Y.
Using grains and Jinja2, we get something like this:
Make sure apache is running:
service.running:
- name: apache2
- enable: True
- require:
- pkg.apache_installation
apache_installation:
pkg.installed:
- name: apache2
install_apache:
pkg.installed:
{% if grains['os_family'] == 'Debian' %}
- name: apache2
{% elif grains['os_family'] == 'RedHat' %}
- name: httpd
{% endif %}
System to give minion-sensitive data.
Pillar data is sensitive data. Holds per-minion data, custom data for each minion.