Search code examples
salt-project

Set up a conditional cmd.run based on file.managed in SaltStack


I have a SaltStack state file (sls) that has a pretty simple state defined.

MyStateRule:
  file.managed:
    - source: salt://scripts/rule.ps1
    - name: 'c:\scripts\rule.ps1'
  cmd.run:
    - name: powershell c:\scripts\rule.ps1
    - require:
      - file: MyStateRule

When I run a state.apply command, the cmd.run appears to execute every time, which I can see makes sense. What I want is to only run when the managed file needs to be copied over to the minion. Can I use file.managed in that case? What do I need to change, such that the script only runs when the file is copied over?


Solution

  • Got it -- rather than using "require," use onchanges:

    cmd.run:
      - name: powershell c:\scripts\rule.ps1
      - onchanges:
        - file: MyStateRule