Score:1

Ansible filter using ad-hoc command

cz flag

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
}
Score:2
nz flag

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
}
U880D avatar
ca flag
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).
Score:2
th flag

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

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.