Austin Bennett
Austin Bennett

Reputation: 11

How do I get just the STDOUT of a salt state?

my output now

I'm learning salt stack right now and I was wondering if there was a way to get the stdout of a salt state and put it into a document and then send it to the master. Or is there a better way to do this?

Upvotes: 0

Views: 2374

Answers (2)

whytewolf
whytewolf

Reputation: 704

The output is already going to the master. It would be better to actually output in json and query down to the data you want in your document on the master.

such as the following

Normal output

$ sudo salt salt00\* state.apply tests.test3                                                                                                                                                 
salt00.wolfnet.bad4.us:
----------
          ID: test_run
    Function: cmd.run
        Name: echo test
      Result: True
     Comment: Command "echo test" run
     Started: 10:39:51.103057
    Duration: 18.281 ms
     Changes:
              ----------
              pid:
                  8661
              retcode:
                  0
              stderr:
              stdout:
                  test

Summary for salt00.wolfnet.bad4.us
------------
Succeeded: 1 (changed=1)
Failed:    0
------------
Total states run:     1
Total run time:  18.281 ms

json output

$ sudo salt salt00\* state.apply tests.test3 --out json                                                                                                                                      
{
    "salt00.wolfnet.bad4.us": {
        "cmd_|-test_run_|-echo test_|-run": {
            "name": "echo test",
            "changes": {
                "pid": 9057,
                "retcode": 0,
                "stdout": "test",
                "stderr": ""
            },
            "result": true,
            "comment": "Command \"echo test\" run",
            "__sls__": "tests.test3",
            "__run_num__": 0,
            "start_time": "10:40:55.582273",
            "duration": 19.374,
            "__id__": "test_run"
        }
    }
}

json parsed down with jq to just the stdout

$ sudo salt salt00\* state.apply tests.test3 --out=json | jq '.|.[]|."cmd_|-test_run_|-echo test_|-run"|.changes.stdout'                                                                     
"test"

Also, for the record it is considered bad practice to put code that changes the system into jinja. Jinja always runs when a template is rendered and there is no way to control if it happens so just running test=true tests will still run the jinja code that makes changes which could be very harmful to your systems.

Upvotes: 2

seshadri_c
seshadri_c

Reputation: 7340

To achieve this, we'll have to save the execution of the script in a variable. It will contain a hash containing keys that are showing up under changes:. Then the contents of this variable (stdout) can be written to a file.

{% set script_res = salt['cmd.script']('salt://test.sh') %}

create-stdout-file:
  file.managed:
    - name: /tmp/script-stdout.txt
    - contents: {{ script_res.stdout }}

Upvotes: 1

Related Questions