6

State file.managed has defaults and context options for template rendering. Both provide context for template vars. What is the difference between them?

Raz
  • 7,508
  • 4
  • 27
  • 25

1 Answers1

7

defaults are the fallback default values that will be passed to the template in case context doesn't have a proper value. If context has a value - it will override default. E.g:

/etc/myconfig.cfg:
   - file.managed:
     - source: salt://myconfig.tmpl
     - template: jinja
     - defaults:
       foo: bar
     - context:
       foo: baz

In this case value of foo will always be baz. Generally context is used when you need to have conditional values. E.g.:

/etc/myconfig.cfg:
   - file.managed:
     - source: salt://myconfig.tmpl
     - template: jinja
     - defaults:
       foo: bar
     {% if salt[grains.get]("os") == 'Debian' %}
     - context:
       foo: baz
     {% endif %}

In this case every non-Debian system will end-up having value bar, while Debian will have baz in the template.

alexK
  • 963
  • 1
  • 7
  • 17
  • 1
    OK. But the same can be archived just with a simple `else`. – Raz Jun 16 '17 at 22:32
  • Yes, that's true. It's mostly a matter of what is more convenient for you and your team. In `salt` there are few other places where you have more than one option to perform the same action like `watch` vs `watch_in` – alexK Jun 18 '17 at 08:34
  • I never understood this two parameters, and still don't, because as said by @Raz you can also do it with only one. Nevermind I'll continue to ignore one of them :) – daks Oct 10 '19 at 13:17