Repository navigation
Description
Based on this stackoverflow question.
Problem
Config in /etc/salt/master:
show_jid: True
Dummy state in /srv/salt/dummy.sls:
dummy_state:
cmd.run:
- name: "echo dummy"
Command:
salt --out json '*' state.sls dummy
Output (for 1 minion):
jid: 20150705154900243813
{
"elephant": {
"cmd_|-dummy_state_|-echo dummy_|-run": {
"comment": "Command \"echo dummy\" run",
"name": "echo dummy",
"start_time": "15:49:04.196608",
"result": true,
"duration": 7.117,
"__run_num__": 0,
"changes": {
"pid": 6501,
"retcode": 0,
"stderr": "",
"stdout": "dummy"
}
}
}
}
This output is not valid JSON - it contains jid.
The problem is that instead of using JSON output directly, one has to create a special parser to weed out all interleaved JSON and non-JSON text before feeding it to the parser. Or, in short, the output is unusable.
Solution
The solution should stem from requirement to have valid requested format on STDOUT no matter what.
Ideally, I can think of an aggregator which collects outputs from everywhere (1 code honouring show_jid, 2 state modules, 3 anything else) and formats it properly on STDOUT based on user request (JSON, YAML, etc.). Otherwise, when each module or feature generates data on its own, it is hard to think about how they can insert their part of text to create valid format as a whole.
Test
load_json.py:
import sys
import json
data = json.load(sys.stdin)
print str(data)
For example, capturing the output above and feeding it into standard JSON parser in Python does not work:
salt --out json '*' state.sls dummy > output.json
Traceback (most recent call last):
File "load_json.py", line 4, in <module>
data = json.load(sys.stdin)
File "/usr/lib64/python2.7/json/__init__.py", line 290, in load
**kw)
File "/usr/lib64/python2.7/json/__init__.py", line 338, in loads
return _default_decoder.decode(s)
File "/usr/lib64/python2.7/json/decoder.py", line 366, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib64/python2.7/json/decoder.py", line 384, in raw_decode
raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded
Activity
uvsmtid commentedon Jul 5, 2015
#25153 is a similar issue specific to results generated by multiple minions.
#14725 is a similar issue specific to
salt-cloud.[-]All data mixed on STDOUT should generate valid JSON output[/-][+]All data mixed on STDOUT together should generate valid JSON output[/+]jfindlay commentedon Jul 6, 2015
@uvsmtid, thanks for the report. Along with #25153, you are welcome to look at the code and help fix these issues or provide other input, thanks.
uvsmtid commentedon Jul 16, 2015
The issue can be considered fixed in
2015.5.3because--staticoption cleans the output producing only result of requested command (there is nojid:). In other words, JSON format is not broken:See also comment here.
jfindlay commentedon Jul 16, 2015
@uvsmtid, thanks for finding that. I did not know that that option existed. Do you think we need better documentation on it? Otherwise I think this issue and #25153 could probably closed.
uvsmtid commentedon Jul 18, 2015
@jfindlay, the only thing I could suggest is to have
--staticbe on by default at least in case of--out=json. It's natural to expect valid JSON by default. However, such change can wait until the problem gets large at scale (many reports and annoys everyone). The documentation is actually correct and has Note section specifically for--out=json- it's just the case when the note only makes sense when you already hit the problem.Maybe changing the first line by the second:
jfindlay commentedon Jul 18, 2015
@uvsmtid, you are welcome to submit a pull request. :-)
rallytime commentedon Sep 9, 2015
@uvsmtid Thanks again for this great explanation as well as for the documentation fix. I am going to go ahead and close this per your comments in #25153.