Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saltstack - how to use Jinja logic based on saltenv

Tags:

salt-project

Is it possible to use Jinja to render a Salt state file, based on the salt environment?

Here's a simple example of setting a timezone for a server. I want to set the timezone to New York if we are in the 'dev' environment, otherwise the timezone should be set to London.

timezone.sls

{% if saltenv == 'dev' %}
America/New_York
{% else %}
Europe/London
{% endif %}
  timezone.system

Executing salt '*' state.sls timezone saltenv='dev' gives the error:

Rendering SLS "base:timezone" failed: Jinja variable 'saltenv' is undefined; line 1

EDIT - as requested, here's the contents of the 'top.sls' file:

base:
  '*':
    - basic
    - git
like image 684
user2761030 Avatar asked Dec 09 '25 07:12

user2761030


1 Answers

If anyone is still looking for an answer to this:

{% set saltenv = salt.config.get('saltenv') %}
{% if saltenv == 'dev' %}
America/New_York
{% else %}
Europe/London
{% endif %}
  timezone.system
like image 157
Denny Avatar answered Dec 12 '25 07:12

Denny