`ansible` ad-hoc command piped to `jq`

Viewed 151

I would like to be able use jq to parse output from ansible -m setup command, but I cannot figure out how to get rid of the non-json output:

192.132.1.1 | SUCCESS =>  {"ansible_facts": ...

How can I suppress the 192.132.1.1 | SUCCESS => output?

1 Answers

I could not find any native flag in the ansible command to suppress the non-json part of the output. Here is the regex based removal of undesired string:

ansible -m setup localhost | sed -r '1s/^.*?=> //g'

Above command would remove the strings like below from the output and the remaining would be a valid JSON.

localhost | SUCCESS => {

or

localhost | FAILED! => 

Eg: Test should jq is happy with the resulted output:

ansible -m setup localhost  |sed -r '1s/^.*?=> //g'|jq -e . &>/dev/null; echo $?
0
Related