How to use facts under ansible_facts as filter in Smart Inventory?

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

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

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.

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

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

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

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

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

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

Ankit,

The casting is an Ansible problem. See 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

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