SaltStack Configuration Management

Get Started Tutorial


You'll learn how to:

  • Use Jinja conditionals
  • Use Jinja loops
  • Call salt execution modules using Jinja

Estimated time: 15 minutes 


Salt includes the Jinja2 templating engine which can be used in Salt state files, Salt pillar files, and other files managed by Salt.

Salt lets you use Jinja to access minion configuration values, grains and Salt pillar data, and call Salt execution modules. This is in additional to the standard control structures and Python data types that are already available in Jinja.


One of the most common uses of Jinja is to insert conditional statements into Salt pillar files.

Because many distros have different package names, you can use the os grain to set platform specific paths, package names, and other values.

For example:

{% if grains['os_family'] == 'RedHat' %}
apache: httpd
git: git
{% elif grains['os_family'] == 'Debian' %}
apache: apache2
git: git-core
{% endif %}

As you can see, Salt grains are available in a dictionary much like Salt pillar. This example checks Salt grain values to set OS specific Salt pillar keys.

Save the snippet above to the saltstack/pillar/common.sls file, and then run the following commands to refresh and then list Salt pillar values for each minions:

salt '*' saltutil.refresh_pillar
salt '*' pillar.items

Your minions should list the values set for the Debian OS family.

After setting these values, when you apply the following Salt state:

install apache:
    - name: {{ pillar['apache'] }}

The httpd package is installed on RedHat, while the apache2 package is installed on Debian.


Loops are useful for creating users and folders in Salt states.

{% for usr in ['moe','larry','curly'] %}
{{ usr }}:
{% endfor %}
{% for DIR in ['/dir1','/dir2','/dir3'] %}
{{ DIR }}:
    - user: root
    - group: root
    - mode: 774
{% endfor %}

In general, you should try to keep your Salt states as simple as possible. If you find yourself writing complex Jinja, you should consider breaking a task into multiple Salt states, or writing a custom Salt execution module for the task (this is easier than it sounds, especially if you know some Python!).

Get Data using Salt

You can call Salt execution functions from Jinja to get real-time data from the system.

{{'whoami') }}