-1

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?

OrangeDog
  • 36,653
  • 12
  • 122
  • 207
boxuan666
  • 11
  • 2
  • You should also use a [`file.rename`](https://docs.saltproject.io/en/latest/ref/states/all/salt.states.file.html#salt.states.file.rename) state instead of what you've written there. Or possibly just the [regular file backups](https://docs.saltproject.io/en/latest/ref/states/backup_mode.html). – OrangeDog Feb 15 '23 at 22:13

2 Answers2

0

{% 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.

OrangeDog
  • 36,653
  • 12
  • 122
  • 207
-1

In simple use case such as shown in your question, you might not even need to set another variable at all. We could directly use {{ java_script_path }}/startup.sh in the if condition. Like below:

{% if salt['file.file_exists']("{{ java_script_path }}/startup.sh") %}
  cmd.run:
    - name: mv {{ java_script_path }}/startup.sh {{ java_script_path }}/startup.sh.backup-$(date +"%Y-%m-%d-%H-%M-%S")
{% endif %}

You could also reconsider using command to backup the file, and use an appropriate Saltstack module instead.

seshadri_c
  • 6,906
  • 2
  • 10
  • 24