0

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.

Example:

enable_foo:
  file.replace:
    - 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.
guettli
  • 3,113
  • 14
  • 59
  • 110
  • out of sheer curiosity I asked how ansible would solve this: https://serverfault.com/a/950406/90324 – guettli Jan 24 '19 at 10:07

1 Answers1

0

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

daks
  • 673
  • 6
  • 23
  • 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 '19 at 13:58
  • This is not a broken state. This is a non-applied state which is quite different. Check https://docs.saltstack.com/en/latest/ref/states/failhard.html for what you want to do. – daks Jan 23 '19 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 '19 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 '19 at 14:31
  • out of sheer curiosity I asked how ansible would solve this: https://serverfault.com/a/950406/90324 – guettli Jan 24 '19 at 10:07