Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

salt-stack

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

like image 664
Raz Avatar asked Jun 16 '17 16:06

Raz


People also ask

What is Salt state file?

The core of the Salt State system is the SLS, or SaLt State file. The SLS is a representation of the state in which a system should be in, and is set up to contain this data in a simple format. This is often called configuration management.

What is salt Highstate?

A “highstate” is a way for Salt to dynamically determine which Salt Formulas should be applied to a certain minion. To start with you execute a “highstate” like this: salt 'minion01' state.highstate. This command causes the Minion to download and examine a file from the Salt Master called the “top file”.


1 Answers

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.

like image 192
alexK Avatar answered Sep 29 '22 23:09

alexK