0

I have an inventory of about 10 hosts. On all I want to check if a specific file exists. So I do:

ansible -m stat -a 'path=/the/file' all

And then I get a load of output because on most servers this file does actually exist.

But I only care for stat.exists for each host.

Is there any possibility of displaying only a specific entry of the result set per host?

And I do not want to use a playbook. This is specifically for ad-hoc queries.

Martin B.
  • 1,567
  • 14
  • 26
  • Pipe to [jq](https://stedolan.github.io/jq/) would do the job if you manage to get output in JSON. Unfortunately `export ANSIBLE_STDOUT_CALLBACK=json` doesn't work properly. – Vladimir Botka Mar 27 '20 at 13:16

1 Answers1

0

For such a trivial test, you can emit a marker string that allow you to filter out all the other junk to just see the host that matches:

ansible -m raw \
   -a 'if [[ -e "/the/file" ]]; then echo "FOUND: $HOSTNAME"; fi' \
   all 2>/dev/null | grep ^FOUND
mdaniel
  • 31,240
  • 5
  • 55
  • 58
  • This does solve this particular problem, but does not answer the question. – Martin B. Mar 29 '20 at 11:46
  • Your question was "Is there any possibility of displaying only a specific entry of the result set per host?" which this does; the middle ground is to use `ansible --one-line` and feed that though `awk -F"|" '/inode/{print $1}'` to grab the host which brought back an inode entry, and the only pure-ansible way is what Vladimir suggested and find out why `ANSIBLE_STDOUT_CALLBACK` doesn't work for `AdhocCLI` – mdaniel Mar 29 '20 at 18:19