# AWX smart inventory with Ansible Local Facts?

**URL:** <https://forum.ansible.com/t/awx-smart-inventory-with-ansible-local-facts/5024>\
**Category:** Get Help\
**Tags:** awx, inventory\
**Created:** [April 18, 2024, 4:10pm UTC](https://forum.ansible.com/t/awx-smart-inventory-with-ansible-local-facts/5024 "2024-04-18T16:10:36Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jpmens](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jpmens/32/5543_2.png) [@jpmens](https://forum.ansible.com/u/jpmens)\
**Post date:** [April 18, 2024, 4:10pm UTC](https://forum.ansible.com/t/awx-smart-inventory-with-ansible-local-facts/5024/1 "2024-04-18T16:10:36Z")

</div>

I’m a great fan of Ansible’s [Local Facts](https://jpmens.net/2013/06/17/a-very-welcome-addition-to-ansible-s-setup-module/) which may stem from the fact that Seth and I had a few beers over the idea just before he wrote the code back in 2013. Yeah, that dates me.

Anyhow, I’m trying to create an AWX smart inventory based off such facts, and have the following data in the fact cache of one of a host:

```json
{
  "ansible_system": "Linux",
  "ansible_local": {
    "nn": "00",
    "huuid": "c62969f0-4a71-4179-93fd-cb9e0cf0d3df",
    "owner": {
      "username": "Jane"
    }
  }
}

```

I can invoke the APi to find Linux hosts with

```auto
/api/v2/hosts/?host_filter=ansible_facts__ansible_system=Linux

```

but no amount of any of this works:

```auto
/api/v2/hosts/?host_filter=ansible_facts __ansible_local__ owner__username=Jane

```

Does anybody know what the magic incantation might be?

(BTW, what worries me is that a search for `"ansible_facts__ansible_local"` in the `ansible/awx` repo returns 0 hits.)

---

<div class="post-metadata">

**Author:** ![kurokobo](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/kurokobo/32/182_2.png) [@kurokobo](https://forum.ansible.com/u/kurokobo)\
**Post date:** [April 18, 2024, 4:39pm UTC](https://forum.ansible.com/t/awx-smart-inventory-with-ansible-local-facts/5024/2 "2024-04-18T16:39:08Z")

</div>

I have only done a very simple test on `localhost`, but it seems to work in my environment.

 ![image](https://us1.discourse-cdn.com/ansible/original/2X/a/af122cb9cfe324dd706175853e6fae670414faf0.png)

```bash
$ curl -sk --user admin: ******** https://awx.example.com/api/v2/hosts/?host_filter=ansible_facts __ansible_local__ owner__username=Jane | jq -r '.results[].name'
localhost

```

Can you see stored value in `ansible_local` in `Facts` tab for your host in your inventory?

---

<div class="post-metadata">

**Author:** ![jpmens](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jpmens/32/5543_2.png) [@jpmens](https://forum.ansible.com/u/jpmens)\
**Post date:** [April 18, 2024, 4:50pm UTC](https://forum.ansible.com/t/awx-smart-inventory-with-ansible-local-facts/5024/3 "2024-04-18T16:50:49Z")

</div>

Thank you, @kurokobo. As usual, just after typing in the question I realized: “beginner error”. I was querying the API in a Web browser which was logged into AWX as a different user as the user whom the inventory belongs to!

I do see the values in the Facts cache (tab for the host), and it works.

---

<div class="post-metadata">

**Author:** ![kurokobo](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/kurokobo/32/182_2.png) [@kurokobo](https://forum.ansible.com/u/kurokobo)\
**Post date:** [April 20, 2024, 2:12pm UTC](https://forum.ansible.com/t/awx-smart-inventory-with-ansible-local-facts/5024/4 "2024-04-20T14:12:30Z")

</div>

> [@jpmens](#):
>
> (BTW, what worries me is that a search for `"ansible_facts__ansible_local"` in the `ansible/awx` repo returns 0 hits.)

AWX uses just `split("__")` and `for` loop without any hard-coded fact names. This is off-topic, though 😃

> <https://github.com/ansible/awx/blob/f178c84728c89616cf9a54823bc87716b17db5b6/awx/main/utils/filters.py#L211-L236>

---

<div class="post-metadata">

**Author:** ![jpmens](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jpmens/32/5543_2.png) [@jpmens](https://forum.ansible.com/u/jpmens)\
**Post date:** [April 20, 2024, 2:17pm UTC](https://forum.ansible.com/t/awx-smart-inventory-with-ansible-local-facts/5024/5 "2024-04-20T14:17:03Z")

</div>

> [@kurokobo](#):
>
> AWX uses just `split("__")` and `for` loop without any hard-coded fact names.

While it might be off-topic, it’s a big help to me; I’ve always wondered why on earth the double underscore was used instead of simply using the period as separator we’ve all been used to.

Your mention of `split("__")` makes it a bit more logical. 😊

---

<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:** [May 20, 2024, 2:17pm UTC](https://forum.ansible.com/t/awx-smart-inventory-with-ansible-local-facts/5024/6 "2024-05-20T14:17:11Z")

</div>

Closed on May 20, 2024, 2:17pm UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
