salt-project

How to pass variable from orchestration into minion state?


The test.sls is the state file I call via orchestrate and give the env variable via pillar. How can I get the env value and passing it to web_check sls file.

test.sls

{% set env = salt.pillar.get('env') %}


{{ env }}_check:
  salt.state:
    - tgt: 'minion-id'
    - sls: web_check     # called sls state file

web_check.sls

{% if env == 'qa' %}

test:
  cmd.run:
    - name: ls -ltra /tmp

{% elif env == 'uat' %}

test:
  cmd.run:
    - name: ls -ltra /tmp/uat

{% endif %}

Execution

salt-run state.orchestrate test pillar='{"env": "qa"}'

Solution

  • Pillar is usually the best way to pass data from orchestration:

    {% set env = pillar["env"] %}  # the value from the master's pillar (via CLI)
    
    {{ env }}_check:
      salt.state:
        - tgt: minion-id
        - sls: web_check
        - pillar:
            env: "{{ env }}"  # overrides the minion's pillar
    
    {% if pillar["env"] == "qa" %}
    
    test:
      cmd.run:
        - name: ls -ltra /tmp
    
    {% elif pillar["env"] == "uat" %}
    
    test:
      cmd.run:
        - name: ls -ltra /tmp/uat
    
    {% endif %}
    

    However, what you're doing here looks exactly like what Salt Environments are for, which you would use the saltenv parameter to select.