Search code examples
jinja2salt-project

Setting out a list of variables in Salt and then importing into another file


Is it possible to set out a list of salt variables such as:

{% set foo = '1234' %}
{% set bar = '10.1.1.2' %}
{% set environment = salt['grains.get']('env') %}

and then import them into separate .sls file and use them like so:

foo_value = {{ foo }} # sets foo to '1234'
bar_value = {{ bar }} # sets bar to '10.1.1.2'
etc...

Solution

  • The best fit should be the import feature. You can store the file with variables as described in question and then import them like:

    {% from 'yourfile.jinja' import foo with context %}
    {% from 'yourfile.jinja' import bar with context %}
    {% from 'yourfile.jinja' import environment with context %}
    

    Or alternatively you can add all of them to an array:

    {% set vars = {
      'foo': '1234',
      'bar': '10.1.1.2',
      'environment': salt['grains.get']('env'),
      }
    %}
    

    And then import it at once:

    {% from 'yourfile.jinja' import vars with context %}
    

    Best practices on using variables (and import) are described at Salt Best Practices page.