Can we use both filter and gather_subset in the setup module at a time?

**Can we use both filter and gather_subset in the setup module at a time? **.

-  name: Check CPU and Memory
   hosts: all
   gather_facts: false
   tasks:
    - setup:
       filter: ansible_*_mb
       gather_subset:
            - processor_count

    - debug:
        msg:
         - "{{ ansible_processor_vcpus }}"
         - "{{ ansible_memtotal_mb }}"

Yes, but your example would produce nothing I assume. You are only gathering data about processor count, and then filtering on that to only include information about memory.

EDIT: To clarify, subsets restrict what data is gathered from the system, filtering filters the gathered information down to specific keys. Filters do not impact what data is gathered, only what is ultimately returned after gathering.

4 Likes

I want to get the managed node CPU info and Memory info, from the above example debug is only printing either of the “{{ ansible_processor_vcpus }}” or “{{ ansible_memtotal_mb }}” but not both.

Try this?

- name: Check CPU and Memory
  hosts: all
  gather_facts: false
  tasks:

    - name: Gather hardware facts
      setup:
        gather_subset:
          - hardware

    - name: Debug processors and memory
      ansible.builtin.debug:
        msg:
         - "CPUs: {{ ansible_processor_vcpus }}"
         - "Memory: {{ ansible_memtotal_mb }}"
1 Like