Make a Million of Those – Jinja Templating Salt States

Do you need a 50 users and Apache name based virtual hosts for each? If you use salt, you can use loops and variables with Jinja templates on your states. This short example configuration creates three text files with different contents, using a loop and variable.
Templates write states in a concise way. Abstracting away repetition also reduces typing errors. DRY – don’t repeat yourself.

Prerequisites: master-slave salt and salt states.

Create a New State Called “multi”

$ sudo mkdir -p /srv/salt/multi/
$ sudoedit /srv/salt/multi/init.sls

You can use jinja to generate salt yaml code. This creates three states, whose state IDs are “/tmp/moikat/foo.txt”, “/tmp/moikat/bar.txt” and “/tmp/moikat/kala.txt”.

{% for file in ['foo.txt', 'bar.txt', 'kala.txt'] %}
/tmp/moikat/{{ file }}:
  file.managed:
    - source: salt://multi/moikka.txt
    - makedirs: True
    - template: jinja
    - context:
      file: {{ file }}
{% endfor %}

Create a File Template

You can also use jinja to generate files from templates. Here, we insert a single variable to file. To use variables in files, the state must have two things set. Setting “template: jinja” enables templating – otherwise the source file is simply copied to slave. Setting “context:” “name: value” sets context variables to jinja. Only context variables are available in jinja file templates.

$ sudoedit /srv/salt/multi/moikka.txt
$ cat /srv/salt/multi/moikka.txt
Moikka
Tämä on {{ file }}

On Jinja Syntax

{% jinja statement %} for..in, endfor, if-else-endif
{{ variable }} Print jinja context variable. Any expression would work.

Test

Always test in small steps. To keep this article brief, we test this all in one go. But in real life, to actually write the state, I tested it in very small steps.

$ sudo salt-call --local state.apply multi

We can see that three files were created to slave. Each file has a different content.

$ head -100 /tmp/moikat/*
==> /tmp/moikat/bar.txt <==
Moikka
Tämä on bar.txt
==> /tmp/moikat/foo.txt <==
Moikka
Tämä on foo.txt
==> /tmp/moikat/kala.txt <==
Moikka
Tämä on kala.txt

Bonus Trick: Render Jinja SLS to Plain SLS

What kind of SLS file is created from this multi/init.sls with jinja template?

$ sudo salt '*' state.show_sls multi --out yaml

The output shows the rendering result

  /tmp/moikat/bar.txt:
    __env__: base
    __sls__: multi
    file:
    - source: salt://multi/moikka.txt
    - makedirs: true
    - template: jinja
    - context: null
      file: bar.txt
    - managed
    - order: 10001
  /tmp/moikat/foo.txt:
    __env__: base
    __sls__: multi
    file:
    - source: salt://multi/moikka.txt
   # ... and lot more

Does 34 line output look annoyingly long? And this is just creating three files.
Using Jinja templates allow us to write states in a concise way. DRY – don’t repeat yourself.
This article has been edited multiple times.

Posted in Uncategorized | Tagged , , , , , , , , , , , , , , | Comments Off on Make a Million of Those – Jinja Templating Salt States

Comments are closed.