1

How can I filter the nocache block or free block using ad-hoc command? I tried ansible centos1 -m setup -a 'filter=ansible_memory_mb.nocache' but doesn't filter it out.

ansible centos1 -m setup -a 'filter=ansible_memory_mb'
centos1 | SUCCESS => {
    "ansible_facts": {
        "ansible_memory_mb": {
            "nocache": {
                "free": 11808,
                "used": 926
            },
            "real": {
                "free": 10686,
                "total": 12734,
                "used": 2048
            },
            "swap": {
                "cached": 0,
                "free": 4096,
                "total": 4096,
                "used": 0
            }
        },
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false
}
user630702
  • 495
  • 10
  • 32

2 Answers2

2

You can't. As the documentation for setup says, "The filter option filters only the first level subkey below ansible_facts."

flowerysong
  • 901
  • 4
  • 6
2

this is a bit messy, but you can..

I am running my ansible against my localhost, so you will have to modify the path a bit, and replace localhost

ANSIBLE_LOAD_CALLBACK_PLUGINS=true ANSIBLE_STDOUT_CALLBACK=json ansible localhost -m setup | jq .plays[].tasks[].hosts.localhost.ansible_facts.ansible_memory_mb.nocache

output:

{
  "free": 11987,
  "used": 703
}
Egidijus
  • 109
  • 1
  • 4
  • Right, after one has the output in JSON format, it will be possible to filter afterwards via [`jq`](https://stedolan.github.io/jq/) (JSON query, like sed for JSON data). – U880D Jan 19 '22 at 20:04