# How to use facts under ansible\_facts as filter in Smart Inventory?

**URL:** <https://forum.ansible.com/t/how-to-use-facts-under-ansible-facts-as-filter-in-smart-inventory/29698>\
**Category:** AWX Project\
**Created:** [February 18, 2019, 1:00pm UTC](https://forum.ansible.com/t/how-to-use-facts-under-ansible-facts-as-filter-in-smart-inventory/29698 "2019-02-18T13:00:24Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ankit2](https://avatars.discourse-cdn.com/v4/letter/a/ea5d25/32.png) [@Ankit2](https://forum.ansible.com/u/Ankit2)\
**Post date:** [February 18, 2019, 1:00pm UTC](https://forum.ansible.com/t/how-to-use-facts-under-ansible-facts-as-filter-in-smart-inventory/29698/1 "2019-02-18T13:00:24Z")

</div>

I have gone through different posts related to this. The API method to set filter for Smart Inventory works on **host variables** however, i am not able to filter the hosts in smart inventory based on **ansible\_facts**.  
I am using the following filter rule in the API section. It is getting set when i run PUT for the filter changes but it doesn’t identify the hosts where AIR\_ID\_32 (custom fact set using set\_fact on host) is present.

“host\_filter”: “ansible\_facts.AIR\_ID\_32\_\_icontains="2000"”

The AIR\_ID\_32 fact will always have a list of values and will be visible under facts section like the following:

AIR\_ID\_32:

- ‘2000’
- ‘2010’

Also, is it possible to exclude specific inventory or target only specific inventory when creating a Smart Inventory?

**Regards,**  
**Ankit**

---

<div class="post-metadata">

**Author:** ![Uriel](https://avatars.discourse-cdn.com/v4/letter/u/53a042/32.png) [@Uriel](https://forum.ansible.com/u/Uriel)\
**Post date:** [February 19, 2019, 5:15pm UTC](https://forum.ansible.com/t/how-to-use-facts-under-ansible-facts-as-filter-in-smart-inventory/29698/2 "2019-02-19T17:15:15Z")

</div>

Have you run a template on the hosts with fact caching enabled? From what I remember, the hosts need to have these facts collected and stored before you can use the variables in a smart inventory filter. I’m not sure how the fact filter works with lists though I know that it only supports equality checks. The Tower Smart Inventory looks like it has a way to get around the equality one matches to provide contains-like functionality: [https://www.ansible.com/blog/ansible-tower-advanced-smart-inventory-usage](https://www.ansible.com/blog/ansible-tower-advanced-smart-inventory-usage)

Essentially, in a playbook, create an indicator fact that is True if “2000” is in the fact, false otherwise.

And from my usage I haven’t found any way to limit a Smart Inventory to a single Inventory.

---

<div class="post-metadata">

**Author:** ![rpetrell](https://avatars.discourse-cdn.com/v4/letter/r/b4bc9f/32.png) [@rpetrell](https://forum.ansible.com/u/rpetrell)\
**Post date:** [February 19, 2019, 10:26pm UTC](https://forum.ansible.com/t/how-to-use-facts-under-ansible-facts-as-filter-in-smart-inventory/29698/3 "2019-02-19T22:26:30Z")

</div>

Ankit,

You can search for exact equality:

“host\_filter”: “ansible\_facts.AIR\_ID\_32="2000"”

…but Django lookup-based filters (like icontains) aren’t currently supported on ansible\_facts because postgres doesn’t give us a way to search these efficiently.

Here’s a related issue:  
[https://github.com/ansible/awx/pull/3152](https://github.com/ansible/awx/pull/3152)

---

<div class="post-metadata">

**Author:** ![harshal2006](https://avatars.discourse-cdn.com/v4/letter/h/a88e57/32.png) [@harshal2006](https://forum.ansible.com/u/harshal2006)\
**Post date:** [March 5, 2019, 10:33am UTC](https://forum.ansible.com/t/how-to-use-facts-under-ansible-facts-as-filter-in-smart-inventory/29698/4 "2019-03-05T10:33:50Z")

</div>

Hi,

with similar requirement - i tried below for ip addresses read x as actual IPs but it didn’t work. Looks like searching list doesn’t work.

“host\_filter”: “ansible\_facts.ansible\_ip\_addresses="x.x.x.x"”, or “host\_filter”: “ansible\_facts\_\_ansible\_ip\_addresses="x.x.x.x"”,

Thanks,  
Harshal

---

<div class="post-metadata">

**Author:** ![Christopher\_Meyers](https://avatars.discourse-cdn.com/v4/letter/c/3e96dc/32.png) [@Christopher\_Meyers](https://forum.ansible.com/u/Christopher_Meyers)\
**Post date:** [March 5, 2019, 1:45pm UTC](https://forum.ansible.com/t/how-to-use-facts-under-ansible-facts-as-filter-in-smart-inventory/29698/5 "2019-03-05T13:45:53Z")

</div>

Glad you have a need for list searching! Use the empty notation [https://github.com/ansible/awx/blob/devel/docs/inventory\_refresh.md#fact-searching](https://github.com/ansible/awx/blob/devel/docs/inventory_refresh.md#fact-searching)

---

<div class="post-metadata">

**Author:** ![harshal2006](https://avatars.discourse-cdn.com/v4/letter/h/a88e57/32.png) [@harshal2006](https://forum.ansible.com/u/harshal2006)\
**Post date:** [March 6, 2019, 6:47am UTC](https://forum.ansible.com/t/how-to-use-facts-under-ansible-facts-as-filter-in-smart-inventory/29698/6 "2019-03-06T06:47:27Z")

</div>

Thanks Christopher it worked with

In another scenario, i am trying to get working using filter (gt - greater than) I couldn’t find any examples for this. Can you please help with example for it? will make life easier.

Thanks,  
Harshal

---

<div class="post-metadata">

**Author:** ![Ankit2](https://avatars.discourse-cdn.com/v4/letter/a/ea5d25/32.png) [@Ankit2](https://forum.ansible.com/u/Ankit2)\
**Post date:** [March 6, 2019, 10:03am UTC](https://forum.ansible.com/t/how-to-use-facts-under-ansible-facts-as-filter-in-smart-inventory/29698/7 "2019-03-06T10:03:51Z")

</div>

In addition to Harshal’s query, i have tried to typecast the values to int where greater than would be required in the smart inventory host\_filter:

**set\_fact:**  
**Pending\_Reboot: false**  
**Missing\_Security\_Patches: “{{ missing\_sp.stdout | int }}”**  
**Missing\_Hotfix\_Patches: “{{ missing\_hfp.stdout | int }}”**

**cacheable: true**

sample value for the set fact for hosts":  
**Missing\_Security\_Patches: ‘0’**

and trying to set the host\_filter as following:  
**“host\_filter”: “ansible\_facts\_\_ansible\_distribution="RedHat" and ansible\_facts. Missing\_Security\_Patches\_\_gt=0”**

Is the issue related to the type of the value for the host (it looks like string even after typecasting it to int in playbook)

**Regards,**  
**Ankit**

---

<div class="post-metadata">

**Author:** ![Christopher\_Meyers](https://avatars.discourse-cdn.com/v4/letter/c/3e96dc/32.png) [@Christopher\_Meyers](https://forum.ansible.com/u/Christopher_Meyers)\
**Post date:** [March 6, 2019, 1:34pm UTC](https://forum.ansible.com/t/how-to-use-facts-under-ansible-facts-as-filter-in-smart-inventory/29698/8 "2019-03-06T13:34:50Z")

</div>

Harshal,

You can only filter on facts using equality. This is for postgres performance reasons related to jsonb types and gin indexes. That being said, I’ve written a blog article on how to work around this limitation [https://www.ansible.com/blog/ansible-tower-advanced-smart-inventory-usage](https://www.ansible.com/blog/ansible-tower-advanced-smart-inventory-usage)

---

<div class="post-metadata">

**Author:** ![Christopher\_Meyers](https://avatars.discourse-cdn.com/v4/letter/c/3e96dc/32.png) [@Christopher\_Meyers](https://forum.ansible.com/u/Christopher_Meyers)\
**Post date:** [March 6, 2019, 3:50pm UTC](https://forum.ansible.com/t/how-to-use-facts-under-ansible-facts-as-filter-in-smart-inventory/29698/9 "2019-03-06T15:50:04Z")

</div>

Ankit,

The casting is an Ansible problem. See [https://github.com/ansible/ansible/issues/15249](https://github.com/ansible/ansible/issues/15249) They have added the ability to have the cast work like you want by setting the ansible setting ANSIBLE\_JINJA2\_NATIVE=true

However, you still aren’t going to be able to do the \_\_gt=0 query. But you can work around this by doing the gt test in Ansible and setting a boolean value accordingly. See [https://www.ansible.com/blog/ansible-tower-advanced-smart-inventory-usage](https://www.ansible.com/blog/ansible-tower-advanced-smart-inventory-usage)

---

<div class="post-metadata">

**Author:** ![harshal2006](https://avatars.discourse-cdn.com/v4/letter/h/a88e57/32.png) [@harshal2006](https://forum.ansible.com/u/harshal2006)\
**Post date:** [March 7, 2019, 10:57am UTC](https://forum.ansible.com/t/how-to-use-facts-under-ansible-facts-as-filter-in-smart-inventory/29698/10 "2019-03-07T10:57:29Z")

</div>

Thanks Chris. For the blog link. Now understood the real purpose of this blog. 🙂 Hope other operators will be available soon in AWX host filters soon.
