Maybe I am blind, but I could not find a way to let the salt-ssh call fail, if file.replace did not replace a single line.

If the pattern I provide did not match, then I want to get noticed.


    - name: /etc/foo/foo.config
    - pattern: DisallowBar
    - repl: AllowBarUntilMidnight
  • Case1: DisallowBar found and replaced, gets replaced: OK
  • Case2: AllowBarUntilMidnight is already in the file: OK
  • Case3: DisallowBar and AllowBarUntilMidnight are not in the file: I want salt to fail.

Saltstack is a configuration management tool so when using states you define what you want, you normally don't bother to be warn when something is done or not.

So in your example I would say "you want replace pattern X by string Y" and that's it. Saltstack does the job to replace the pattern if it exists and do nothing if not.

Moreover, in configuration management, you generally want states to be 'idempotent'. It means successive executions must produce the same result. Wether you execute your state one time or 1000 times should be the same. I'm not sure that what you want to do is really provided by saltstack.

update: of course you can do what you want with saltstack, but you may have to separate in two states: one to check current state and eventually fail (check failhard https://docs.saltstack.com/en/latest/ref/states/failhard.html), and another to replace

  • I like the Zen of Python #10: Errors should never pass silently. If there is a broken state, I want to get a error. About 'idempotent', that is case2 of the question. – guettli Jan 23 at 13:58
  • This is not a broken state. This is a non-applied state which is quite different. Check docs.saltstack.com/en/latest/ref/states/failhard.html for what you want to do. – daks Jan 23 at 14:02
  • case3 is a broken state for me, and I want to get an error if the system is in this state. Yes, I could use a second state with failhard after the file.replace state. This would be a work-around. – guettli Jan 23 at 14:09
  • I looked but didn't find anything so you may have to use a cmd.run or cmd.script with grep – daks Jan 23 at 14:31
  • out of sheer curiosity I asked how ansible would solve this: serverfault.com/a/950406/90324 – guettli Jan 24 at 10:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.