Fair enough.
Here's my end goal in this thread:
* check if file '/tmp/flagf' contains_regex 'foo'
* if it doesn't, then do nothing
* if it does, run a given command ('echo hello' will do for our purposes).
Solutions seen so far were:
* cmd.run with an 'onlyif' that calls grep. This is fine, but I am
trying to avoid the bash/grep forks, even if only for academic
interest for now (i.e., it's not hurting me but I would like to learn
perhaps more elegant ways to do this)
* wrapping the whole thing in a jinja "if". This, as Sam pointed
out, is subject to failure if the condition to be tested was expected
to be setup by an earlier state, because jinja processing happens
before that.
I am looking for a way to make that test happen within salt (using
file.contains_regex) and condition the cmd.run on that.
Here's the code I tried so far, but it always runs the "echo hello",
regardless of whether the file contains the pattern or not.
Even if you don't need to post code, perhaps you could tell me what I
am missing here?
grep:
module.run:
- name: file.contains_regex
- path: /tmp/flagf
- regex: foo
say_hello:
cmd.wait:
- name: echo hello
- cwd: /
- watch:
- module: grep
Here's the output when the file /tmp/flagf contains the pattern "foo":
----------
State: - module
Name: file.contains_regex
Function: run
Result: True
Comment: Module function file.contains_regex executed
Changes: ret: True
----------
State: - cmd
Name: echo hello
Function: wait
Result: True
Comment: Command "echo hello" run
Changes: pid: 17191
retcode: 0
stderr:
stdout: hello
and here's the output when the file does not contain the text:
----------
State: - module
Name: file.contains_regex
Function: run
Result: True
Comment: Module function file.contains_regex executed
Changes: ret: False
----------
State: - cmd
Name: echo hello
Function: wait
Result: True
Comment: Command "echo hello" run
Changes: pid: 17236
retcode: 0
stderr:
stdout: hello
As you can see, the "ret:" dutifully changes from True to False, but
the "Result" is always True. How do I make it reflect the "ret"?
sitaram