Answers: <p>You might want to consider adding an <a href="https://docs.saltstack.com/en/latest/ref/states/requisites.html#unless" rel="nofollow noreferrer"><code>unless</code> or <code>onlyif</code></a> requisite in the first directive - especially if script.sh is particularly complicated or expensive. With <code>unless</code> or <code>onlyif</code>, you will use the reults of several BASH commands to determine if you need to push down and execute your script. If your system is out of plumb only then is the state enforced. If everything checks out OK, then the script will never be pushed and executed preventing <code>changed=2</code> in the final status report. You will want to use a <a href="https://docs.saltstack.com/en/latest/ref/states/requisites.html#require" rel="nofollow noreferrer">require</a> requisite for the second and third directives to prevent them from running if the script is not pushed by <code>file.managed</code> otherwise you will get to errors instead of two changes.</p>
  
Answers: CC BY-SA 3.0
Answers: 2017-08-08T08:11:01.873Z
Answers: 2017-08-08T08:11:01.873Z
Answers: James Shewey
Body: <p>I am doing some operations with Kafka via salt, and using scripts to perform them.</p>
  
  <p>My current SLS file looks roughly like:</p>
  
  <pre><code>/tmp/script.sh:
    file.managed:
      - source: salt://blah/blah
  
  Run script:
    cmd.run:
      - name: /tmp/script.sh
  
  delete /tmp/script.sh:
    file.absent
      - name: /tmp/script.sh
  </code></pre>
  
  <p><code>script.sh</code> is idempotent itself. But I always get a <code>changed=2</code> status report at the end of applying the state, because of it putting the script down and then deleting it.</p>
  
  <p>Is there a more idiomatic way in Salt to have temporary scripts that don't live on the target box? Or do people usually just leave them in /tmp so that they don't see a reported change when applying a state.</p>
  
Tags: <salt>
Title: SaltStack: Use Temporary Scripts And Not Cause State Changes To Reported
UserDisplayName: ThwompWamp
ThreadText: # SaltStack: Use Temporary Scripts And Not Cause State Changes To Reported
  
  (Asked by: username_0 on 2017-08-07)
  
  I am doing some operations with Kafka via salt, and using scripts to perform them.
  
  My current SLS file looks roughly like:
  
  ```
  /tmp/script.sh:
    file.managed:
      - source: salt://blah/blah
  
  Run script:
    cmd.run:
      - name: /tmp/script.sh
  
  delete /tmp/script.sh:
    file.absent
      - name: /tmp/script.sh
  
  ```
  
  `script.sh` is idempotent itself. But I always get a `changed=2` status report at the end of applying the state, because of it putting the script down and then deleting it.
  
  Is there a more idiomatic way in Salt to have temporary scripts that don't live on the target box? Or do people usually just leave them in /tmp so that they don't see a reported change when applying a state.
  
  **Tags:** salt
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2017-08-08. Score: 0 | Accepted answer
  
  You might want to consider adding an [`unless` or `onlyif`](https://docs.saltstack.com/en/latest/ref/states/requisites.html#unless) requisite in the first directive - especially if script.sh is particularly complicated or expensive. With `unless` or `onlyif`, you will use the reults of several BASH commands to determine if you need to push down and execute your script. If your system is out of plumb only then is the state enforced. If everything checks out OK, then the script will never be pushed and executed preventing `changed=2` in the final status report. You will want to use a [require](https://docs.saltstack.com/en/latest/ref/states/requisites.html#require) requisite for the second and third directives to prevent them from running if the script is not pushed by `file.managed` otherwise you will get to errors instead of two changes.
  
  
  ---
  
  