execute command with test condition in salt

execute a command if test is true

append a new line to file if it exist

"echo 'newline' >> /tmp/file.txt":
  cmd.run:
    - onlyif: test -e /tmp/file.txt

execute a command if test is false

create a new file if it not exist using onlyif:

"date > /tmp/date.txt":
  cmd.run:
    - onlyif: test ! -e /tmp/date.txt

create a new file if it not exist using unless:

"date > /tmp/date.txt":
  cmd.run:
    - unless: test -e /tmp/date.txt

Reference: https://docs.saltstack.com/en/latest/ref/states/all/salt.states.cmd.html

Leave a comment