cmd.run which suppresses stdout if successful

184 views
Skip to first unread message

Thomas Güttler

unread,
May 17, 2018, 5:37:17 AM5/17/18
to Salt-users

Is there a way to suppress stdout if the command was successful and print stdout, if it failed?

I tried this, but the first state does print to stdout.


no_stdout_should_be_visible:
  cmd.run:
    - name: cat /etc/fstab
    - output_loglevel: quiet

stdout_should_be_visible:
  cmd.run:
    - name: cat /etc/fstab /does-not-exist
    - output_loglevel: quiet

Regards,
  Thomas

Florian Ermisch

unread,
May 17, 2018, 7:52:25 AM5/17/18
to salt-...@googlegroups.com
Hi Thomas,

you could just use `chronic` from the
moreutils package:
"chronic: runs a command quietly unless it fails"
https://joeyh.name/code/moreutils/

Should only be a `pkg.install moreutils`
away.

Regards, Florian

Thomas Güttler

unread,
May 18, 2018, 3:42:03 AM5/18/18
to Salt-users
Hi Florian,

first I thouht chronic is an additional module for salt.
But it is a general purpose command line tool.
The command "chronic" was new to me.
It is available on Ubunt, but unfortunately not directly available
for SuSE-Linux which I need to support.
I could write a simple replacement for chronic with a view python
lines ....

Nevertheless, thank you very much for your help

Regards,
  Thomas

Ethan Erchinger

unread,
May 18, 2018, 11:07:42 AM5/18/18
to Salt-users
Redirect stdout/stderr to a file with an onfail requisite that cats that file, or success requisite that deletes the file.

Jeremy McMillan

unread,
May 21, 2018, 1:29:12 PM5/21/18
to Salt-users
It seems to me that you want to do things which might be accomplished better using a different state module than cmd.run.

If not, maybe you should consider cmd.script, and put a literal script in your state, which handles the output as you intend? Also look into helping salt understand the results better by using stateful=true, and capturing the output and state in your script.

Thomas Güttler

unread,
May 23, 2018, 2:52:55 AM5/23/18
to Salt-users
I like the idea of Florian Ermisch (use command line tool chronic).
Since this tool is not available on my plattform, I can use cmd.script to upload a simple python script
which implements chronic.

Problem solved. Thank you all, for your feedback!
Reply all
Reply to author
Forward
0 new messages