Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saltstack variables

I have the question about variables in salt. I am trying to use the if statements to create more complex states with salt.

example working:

{% set old_stable = salt['cmd.run']('cd /home/project_name && ls -t|grep 2|grep -v tar.gz|tail -n +2|head -n 1') %}
{% set time_date = salt['cmd.run']('date +%Y%m%d%H%M') %}
{% if salt['cmd.run']('ls -lt /home/project_name/ | wc -l') == 2 %}
      <STATE>
{% endif  %}

So, the question is: Can I define "/home/project_name/" like variable like {{ old_stable }}to put on top of file

Inserting of variable in if statement doesn't work

example(not working)

{% set project = '/home/project_name' %}
 {% if salt['cmd.run']('ls -lt {{ project }}') | wc -l') == 2 %}
       <STATE>
 {% endif  %}

My code is

{% set project = 'test_web_tool' %}

{% if salt['cmd.run']('ls -lt /home/project-user/project 2>/dev/null| wc -l') != "0" %}

output:
 cmd.run:
     - names:
       - echo "Rollback directory {{ project }}"
     - cwd: /root

{% else %}

error_output:
 cmd.run:
     - names:
       - echo "This is the last directory. Cant remove it"
    - cwd: /root

{% endif  %}
like image 208
user3323536 Avatar asked May 30 '26 20:05

user3323536


1 Answers

You probably want to use ~ operator to concatenate two strings:

{% set project = '/home/project_name' %}
{% if salt['cmd.run']('ls -lt ' ~ project ~ ' | wc -l') == 2 %}
    <STATE>
{% endif %}
like image 152
quanta Avatar answered Jun 02 '26 09:06

quanta