Red Cricket
Red Cricket

Reputation: 10470

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

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?

Upvotes: 2

Views: 544

Answers (1)

P....
P....

Reputation: 18391

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

Upvotes: 2

Related Questions