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.
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.
…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.
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.
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)
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