Ansible filter using ad-hoc command

Viewed 539

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
}
4 Answers

The same question a had some months ago, but the answer is if you need access to specific internal block, you need to use a Ansible Playbook, unfortunately you can not do it with ad-hoc commands. For example in date time of your localhost:

ansible -m setup localhost -a 'filter=ansible_date_time'

Will return a lot of specific information, like seconds, year, minute, and others. And if you want only return the format date like '2021-10-16' you need to use a playbook. Here some specific playbook to create a folder with date specific format:

  tasks:
    - name: Collect Year, Month and Day.
      setup:
        filter: "ansible_date_time"
        gather_subset: "!all"
    
    - name: Put today's date in a variable.
      set_fact:
        DTG: "{{ ansible_date_time.date }}"

    - name: Create directory with the following path C:\bkp_"year-month-day"\Switches\
      file:
        path: /mnt/c/bkp_{{ hostvars.localhost.DTG }}/Switches/
        state: directory

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"
}

I don't know if it is available without a manipulated script. for a quick solution, you can run below adhoc commands

ansible centos -m setup -a 'filter=ansible_memtotal_mb'
ansible centos -m setup -a 'filter=ansible_memfree_mb'

Quoting from the documentation:

This module is automatically called by playbooks to gather useful variables about remote hosts that can be used in playbooks. It can also be executed directly by /usr/bin/ansible to check what variables are available to a host.

Different parameters like subset, filter, limit the information that is gathered from hosts. Such data is stored in ansible_facts dict.

And, most importantly:

The filter option filters only the first level subkey below ansible_facts

Although, when setup module is run from playbook (or as gather_facts: true), we can access the facts and its subkeys/elements because its available as a variable.

A very simple playbook can achieve what you want. Example:

# play.yml

- hosts: "{{ my_hosts }}"
  gather_facts: false

  tasks:
    - name: filter memory facts
      setup:
        filter: ansible_memory_mb

    # note that an intermediate set_fact task is not required as such
    - name: show memory nocache values
      debug:
        var: ansible_memory_mb.nocache

Then run it instead of ad-hoc command (on centos1):

ansible-playbook play.yml -e "my_hosts=centos1" -i <inventory-path>
Related