ansible

Ansible filter using ad-hoc command


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
}

Solution

  • If you want to try with the ansible command, you have to mix with grep and head:

    ansible centos -m setup -a 'filter=ansible_memory_mb' | grep -Eo [0-9]+ | head -1
    

    but you should use playbook: the var result will contain the value desired.

    - name: test
      hosts: centos1
      
      tasks:
    
        - name: set vars
          set_fact: result="{{ ansible_memory_mb.nocache.free}}" 
    
        - name: show
          debug:
            var: result
    

    Result:

    TASK [show] ***********************************************
    ok: [localhost] => {
        "result": "712"
    }