Search code examples
salt-project

What is the difference between defaults and context options in file.managed salt state?


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


Solution

  • 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.