state.apply cmd.script output not honoring --output=txt?

55 views
Skip to first unread message

Joi Owen

unread,
Aug 12, 2019, 2:10:14 PM8/12/19
to Salt-users
I've written a state that uses cmd.script to do some status checks on minions.  The script is sent and runs just fine.  My issues is, if I specify an outputter, I can only get json outputs, not the plain text I actually want. 

policycheck.sls contains:

policycheck:
  cmd.script:
  - source: salt://bitdefender/files/policycheck.sh
  - runas: root
  - onlyif: test -d /opt/BitDefender/bin


The policycheck.sh script ultimately echos a line or two of text and exists with a return code for pass or fail results.  This works as I expect when run in a bash shell directly on a minion.

If I run it from the salt master with:

salt MINION state.apply bitdefender.policycheck

I get the expected results as:

MINION
----------
          ID: policycheck
    Function: cmd.script
      Result: True
     Comment: Command 'policycheck' run
     Started: 12:31:14.574542
    Duration: 14640.925 ms
     Changes:   
              ----------
              pid:
                  147315
              retcode:
                  0
              stderr:
              stdout:
                  OK MINION Policy appears to be for Linux

Summary for MINION
------------
Succeeded: 1 (changed=1)
Failed:    0
------------
Total states run:     1
Total run time:  14.641 s



But all I want is the stdout value, which I routinely access with cmd.run using --output=txt:

ssalt '*' cmd.run 'ps -ef | grep mongo' --output=txt

and I get back something that looks like:

MINION: mongodb    1151      1  0 Feb16 ?        22:35:12 /usr/bin/mongod --config /etc/mongodb.conf
MINION: unifi      4033   2920 35 Feb16 ?        62-20:11:19 bin/mongod --dbpath /usr/lib/unifi/data/db --port 27117 --unixSocketPrefix /usr/lib/unifi/run --logappend --logpath /usr/lib/unifi/logs/mongod.log --nohttpinterface --bind_ip 127.0.0.1


I find that if I use the txt outputter with other state.apply attempts, I also get a bunch of ugly json back.

Using --output=txt is plainly documented in the salt command documentation.  

What I want from this is to apply the bitdefender.policycheck state on all minions and get back a flat text list of OK/NOT OK outputs from each minion.

Am I misunderstanding how to accomplish this?  Why does --output=txt only output json instead of plain text as requested? Do I need to write a parser of my own to make this behave like cmd.run does?



Phipps, Thomas

unread,
Aug 12, 2019, 2:25:48 PM8/12/19
to salt-...@googlegroups.com
It makes more sense when you realize that json is txt. the data structure is returned by states as a python object, which gets turned into a json object. It just isn't being parsed like it normally would and is just being passed as txt directly back to the output.

If you want to not output the json object you need to write an outputer that first parses the json into a txt format you are expecting.

--
You received this message because you are subscribed to the Google Groups "Salt-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to salt-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/salt-users/2dcd3ef0-9d50-482d-a314-0e766bfa1a4d%40googlegroups.com.

Joi Owen

unread,
Aug 12, 2019, 3:00:27 PM8/12/19
to Salt-users
Is there a way I can get state.apply to use the text outputter used by the cmd.run module?
To unsubscribe from this group and stop receiving emails from it, send an email to salt-...@googlegroups.com.

Phipps, Thomas

unread,
Aug 12, 2019, 3:10:50 PM8/12/19
to salt-...@googlegroups.com
It is using the txt outputter used by cmd.run. The issue is the data that is returned by each. cmd.run outputs just the return of the command. but state.apply is returning the json object of the return data.

To unsubscribe from this group and stop receiving emails from it, send an email to salt-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/salt-users/17da0287-1956-4624-bb92-a5060fc6e571%40googlegroups.com.

Salty Vagrant

unread,
Aug 13, 2019, 8:15:40 AM8/13/19
to Salt-users

Perhaps simplest solution is to pass returned JSON through jq (https://stedolan.github.io/jq/) to isolate the output into simple text as you want.

To unsubscribe from this group and stop receiving emails from it, send an email to salt-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/salt-users/17da0287-1956-4624-bb92-a5060fc6e571%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages