Reputation: 11
I need to apply a if-else logic based on a static minion id inside a state file. The target glob qualifies a whole bunch of servers, but I need to run a small piece of logic on a single server and run a bunch of common things on all of them. How can I do this?
When I put this in a Jinja file, its errors:
{% import salt.config %}
{% minion_opts = salt.config.minion_config('/etc/salt/minion') %}
{% print(minion_opts['id']) %}
{% if minion_opts['id'] == 'xyz.server': %}
-- run the logic here
..
..
Error:
- Rendering SLS 'base:bin.test' failed: Jinja syntax error: expected token 'as', got 'end of statement block'; line 1
-
- ---
- {% import salt.config %} <======================
- {% minion_opts = salt.config.minion_config('/etc/salt/minion') %}
- {% print(minion_opts['id']) %}
It probably goes without saying I am not a Saltstack expert by any means.
Upvotes: 1
Views: 2990
Reputation: 73
So first off, I realize this is an old question, but it still shows up at the top of the my search results for both DDG and Google for a somewhat related problem and the current answers don't really deliver on the ask, so here's an answer based off the latest SaltStack version (3002):
In order to get control flow working for Jinja based off minion id:
{% if salt['grains.get']('id') == 'minion_a' %}
some text to render for minion_a
{% else %}
some text to render otherwise
{% endif %}
I realize there were other issues in how the question was phrased and for the sample code provided, but my code snippet answers the actual question of:
I need to apply a if-else logic based on a static minion id inside a state file.
How can I do this?
Upvotes: 3
Reputation: 612
The error occurs because you're using Jinja's import
in an unexpected way. If this is a Flask app, you should use something like this in your views.py route to provide variables to the template:
render_template('my_template.html', salt=salt)
The corrected code should look something like this:
{% set minion_opts = salt.config.minion_config('/etc/salt/minion') %}
{{ minion_opts['id'] }}
{% if minion_opts['id'] == 'xyz.server' %}
{{ 'logic goes here' }}
{% endif %}
See the Assignments docs on how to assign values to variables.
No colon is necessary at the end of the if
statement, and remember to use {% endif %}
after you're done with your conditional statements.
Upvotes: 0