# Smart Inventory based on group with children

**URL:** <https://forum.ansible.com/t/smart-inventory-based-on-group-with-children/38151>\
**Category:** AWX Project\
**Created:** [June 23, 2023, 9:01am UTC](https://forum.ansible.com/t/smart-inventory-based-on-group-with-children/38151 "2023-06-23T09:01:30Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Christian\_Garling](https://avatars.discourse-cdn.com/v4/letter/c/ec9cab/32.png) [@Christian\_Garling](https://forum.ansible.com/u/Christian_Garling)\
**Post date:** [June 23, 2023, 9:01am UTC](https://forum.ansible.com/t/smart-inventory-based-on-group-with-children/38151/1 "2023-06-23T09:01:30Z")

</div>

Hi there,

I am trying to create a smart inventory based upon the following:

I have a hosts file in INI format with two groups specified as:

[internal:children]  
group1  
group2

[dmz:children]  
group3  
group4

I want to use these groups as a smart inventory each, but if I use the name “internal” or “dmz” in the advanced filter as groups\_\_name it does not look up any hosts of the subgroups. How can I achieve this?

Regards, Christian

---

<div class="post-metadata">

**Author:** ![AWX\_Project](https://avatars.discourse-cdn.com/v4/letter/a/eada6e/32.png) [@AWX\_Project](https://forum.ansible.com/u/AWX_Project)\
**Post date:** [June 23, 2023, 5:12pm UTC](https://forum.ansible.com/t/smart-inventory-based-on-group-with-children/38151/2 "2023-06-23T17:12:12Z")

</div>

Hi,

Smart inventories are being deprecated in favor of the new constructed inventory type. I’d recommend trying to utilize that for more advanced query options.

[https://github.com/ansible/awx/blob/devel/docs/inventory/constructed\_inventory.md](https://github.com/ansible/awx/blob/devel/docs/inventory/constructed_inventory.md)

Thanks,

AWX Team

---

<div class="post-metadata">

**Author:** ![Christian\_Garling](https://avatars.discourse-cdn.com/v4/letter/c/ec9cab/32.png) [@Christian\_Garling](https://forum.ansible.com/u/Christian_Garling)\
**Post date:** [June 26, 2023, 7:37am UTC](https://forum.ansible.com/t/smart-inventory-based-on-group-with-children/38151/3 "2023-06-26T07:37:25Z")

</div>

Hi AWX Team,

thanks for your quick response. Actually I dont see, how a constructed inventory can replace a smart inventory.

With smart inventory I can build smaller sub-inventories with for example only NFS or FTP servers in it, based on the groups in my inventory. I have to use groups, because the hostnames unfortunately do not match their function.

With constructed inventories it seems like they always import all hosts and groups found in the inventory file and just can build further groups based on rules. Thats nice, but groups are not visible / selectable in the Template dialog. So the user needs to know the group names or has to find out before he launches a Template. With the smart inventories its much easier, because the user can select one of them directly in the dialog.

Regards, Christian

---

<div class="post-metadata">

**Author:** ![disi](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/disi/32/1138_2.png) [@disi](https://forum.ansible.com/u/disi)\
**Post date:** [June 26, 2023, 8:15am UTC](https://forum.ansible.com/t/smart-inventory-based-on-group-with-children/38151/4 "2023-06-26T08:15:08Z")

</div>

Hi,  
You would need to write extra code to filter for groups.  
Use as source the inventory with the groups you want to filter.  
i.e. —

plugin: constructed# true means if any of the checks return ‘false’ the whole sync fails  
strict: false

# might not be necessary

use\_extra\_vars: true

# check if Linux OS

compose:

# general check if any of the group name contain certain keywords

linux\_group: (group\_names | regex\_findall(‘._ubuntu._|._redhat._|._rhel._|._debian._|._centos._|._sles._|._suse._|._oraclelinux._|._azure\_os\_linux._’)) | length \>= 1  
groups:

# create the “filter/limit” group[s] ‘azure\_loc\_westus2’ comes from the source inventory

az\_linux\_eastwestus2\_patchwave2: ((group\_names | intersect([‘azure\_loc\_westus2’, ‘azure\_loc\_eastus2’, ‘tag\_patchwave\_2’])) | length \>= 2) and linux\_group

You can then limit for ‘az\_linux\_eastwestus2\_patchwave2’

Disadvantage with constructed inventories, at the moment it will duplicate every host that matches, by creating a new host in this new constructed inventory. Even in the same organisation.

Cheers,

Marcus

---

<div class="post-metadata">

**Author:** ![AWX\_Project](https://avatars.discourse-cdn.com/v4/letter/a/eada6e/32.png) [@AWX\_Project](https://forum.ansible.com/u/AWX_Project)\
**Post date:** [June 28, 2023, 6:49pm UTC](https://forum.ansible.com/t/smart-inventory-based-on-group-with-children/38151/5 "2023-06-28T18:49:27Z")

</div>

Thanks Marcus for providing those instructions! Christian were you able to get it working with constructed inventories?

AWX Team

---

<div class="post-metadata">

**Author:** ![Christian\_Garling](https://avatars.discourse-cdn.com/v4/letter/c/ec9cab/32.png) [@Christian\_Garling](https://forum.ansible.com/u/Christian_Garling)\
**Post date:** [July 14, 2023, 11:44am UTC](https://forum.ansible.com/t/smart-inventory-based-on-group-with-children/38151/6 "2023-07-14T11:44:42Z")

</div>

Hi all,

I had to postpone this in favour for another task. I will try it again, when I have the time to do so.

Regards, Christian
