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 %}
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 %}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With