# How to use multiple filter in setup module

**URL:** <https://forum.ansible.com/t/how-to-use-multiple-filter-in-setup-module/31890>\
**Category:** Ansible Project\
**Created:** [January 2, 2020, 10:57am UTC](https://forum.ansible.com/t/how-to-use-multiple-filter-in-setup-module/31890 "2020-01-02T10:57:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vikrant\_Agarwal](https://avatars.discourse-cdn.com/v4/letter/v/8baadc/32.png) [@Vikrant\_Agarwal](https://forum.ansible.com/u/Vikrant_Agarwal)\
**Post date:** [January 2, 2020, 10:57am UTC](https://forum.ansible.com/t/how-to-use-multiple-filter-in-setup-module/31890/1 "2020-01-02T10:57:22Z")

</div>

I want to extract only specific information of the systems using Ansible setup module , like ansible\_memtotal\_mb , ansible\_machine and ansible\_distribution\_version . How to do that in single ansible command ?

Regards  
Vikrant

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [January 2, 2020, 11:45am UTC](https://forum.ansible.com/t/how-to-use-multiple-filter-in-setup-module/31890/2 "2020-01-02T11:45:01Z")

</div>

It's possible to 'loop' the 'filter' in the module 'setup'  
[https://docs.ansible.com/ansible/latest/modules/setup\_module.html](https://docs.ansible.com/ansible/latest/modules/setup_module.html)

For example:

&nbsp;&nbsp;- hosts: test\_01  
&nbsp;&nbsp;&nbsp;&nbsp;gather\_facts: false  
&nbsp;&nbsp;&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- setup:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;filter: "{{ item }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- 'ansible\_memtotal\_mb'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- 'ansible\_machine'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- 'ansible\_distribution\_version'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var: hostvars.test\_01.ansible\_facts

gives

&nbsp;&nbsp;ok: [test\_01] =\>  
&nbsp;&nbsp;&nbsp;&nbsp;hostvars.test\_01.ansible\_facts:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;distribution\_version: '12.0'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;machine: i386  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;memtotal\_mb: 3400

It's not possible to create single 'filter' because of the limited  
functionality of 'fnmatch', I think  
[https://docs.python.org/3.8/library/fnmatch.html](https://docs.python.org/3.8/library/fnmatch.html)

HTH,

&nbsp;&nbsp;-vlado

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [January 2, 2020, 12:06pm UTC](https://forum.ansible.com/t/how-to-use-multiple-filter-in-setup-module/31890/3 "2020-01-02T12:06:52Z")

</div>

FWIW. Smarter approach to avoid the burden of collecting the facts on each  
play is 'Caching facts'  
[https://docs.ansible.com/ansible/latest/user\_guide/playbooks\_variables.html#caching-facts](https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#caching-facts)

HTH,

&nbsp;&nbsp;-vlado

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [January 13, 2020, 2:50pm UTC](https://forum.ansible.com/t/how-to-use-multiple-filter-in-setup-module/31890/4 "2020-01-13T14:50:11Z")

</div>

FYI, filter does not prevent gathering of other facts, it just filters  
the result, running in loop gathers facts multiple times, it is much  
more efficient to gather 1 time and then extract the subset you  
desire.
