Search code examples
jinja2salt-project

how can i use jinja template and pillar in saltstack states


there is my sls file:

  {% set java_script_path = salt['pillar.get']('script_path', default='/opt/java-app') %}

  {% if salt['pillar.get']('script_path') %}
  {% set file = {{ java_script_path }}/startup.sh %} ## seem this line have Jinja syntax error
  
  {% if salt['file.file_exists']('{{ file }}') %}
  cmd.run:
    - name: mv {{ java_script_path }}/startup.sh {{ java_script_path }}/startup.sh.backup-$(date +"%Y-%m-%d-%H-%M-%S")
  {% endif %}
  {% endif %}

is using salt['pillar.get']('script_path') can not split other string?

example: name: {{ salt['pillar.get']('script_path') }}/startup.sh will raise error like: failed: Jinja syntax error: expected token ':', got '}' how can i fix ?

can you help me to fix my sls file to work?


Solution

  • {% already starts a Jinja context. You do not need to try to start another one with {{.

    {% set file = java_script_path ~ "/startup.sh" %}
    

    It was expecting a : because { starts a dict literal.