# Can inventory plugin "constructed" create a groups based on ansible\_facts

**URL:** <https://forum.ansible.com/t/can-inventory-plugin-constructed-create-a-groups-based-on-ansible-facts/31047>\
**Category:** Ansible Project\
**Tags:** aws\
**Created:** [August 15, 2019, 1:53pm UTC](https://forum.ansible.com/t/can-inventory-plugin-constructed-create-a-groups-based-on-ansible-facts/31047 "2019-08-15T13:53:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![user89](https://avatars.discourse-cdn.com/v4/letter/u/0ea827/32.png) [@user89](https://forum.ansible.com/u/user89)\
**Post date:** [August 15, 2019, 1:53pm UTC](https://forum.ansible.com/t/can-inventory-plugin-constructed-create-a-groups-based-on-ansible-facts/31047/1 "2019-08-15T13:53:08Z")

</div>

Hello community!

A have plenty of EC2 servers in AWS and want to categorize them based on gathered from ansible\_facts (by module setup), for example have group “CentOS” where ansible\_facts[‘distribution’] == ‘CentOS’ and so on. But having file “inventory/constructed.yml”:

plugin: constructed  
strict: False  
keyed\_groups:

# this creates a group per distro (distro\_CentOS, distro\_Debian) and assigns the hosts that have matching values to it,

# using the default separator “\_”

- prefix: distro  
key: ansible\_distribution

I don’t see this group “distro\_CentOS” in output command “ansible-inventory --graph”.

Also this plugin enabled in ansible.cfg:

[inventory]

# enable inventory plugins, default: ‘host\_list’, ‘script’, ‘auto’, ‘yaml’, ‘ini’, ‘toml’

enable\_plugins = host\_list, ini, aws\_ec2, **constructed**

How I can sort instances EC2 based on gathered ansible’s facts, avoiding tag each instance?

---

<div class="post-metadata">

**Author:** ![Gert\_van\_den\_Berg](https://avatars.discourse-cdn.com/v4/letter/g/b77776/32.png) [@Gert\_van\_den\_Berg](https://forum.ansible.com/u/Gert_van_den_Berg)\
**Post date:** [September 18, 2019, 9:00am UTC](https://forum.ansible.com/t/can-inventory-plugin-constructed-create-a-groups-based-on-ansible-facts/31047/2 "2019-09-18T09:00:02Z")

</div>

My guess here is that the inventory gets generated before the setup module runs, which means that the inventory plugin can’t be used for that.

You should be able to get the desired effect using [https://docs.ansible.com/ansible/latest/modules/group\_by\_module.html](https://docs.ansible.com/ansible/latest/modules/group_by_module.html)

- group\_by:  
key: distro\_{{ ansible\_distribution }}

---

<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:** [September 18, 2019, 8:44pm UTC](https://forum.ansible.com/t/can-inventory-plugin-constructed-create-a-groups-based-on-ansible-facts/31047/3 "2019-09-18T20:44:14Z")

</div>

yes, but you need to have facts already. This can be accomplished in 2  
ways, 1) fact cache 2) meta: refresh\_inventory after fact gathering

---

<div class="post-metadata">

**Author:** ![user89](https://avatars.discourse-cdn.com/v4/letter/u/0ea827/32.png) [@user89](https://forum.ansible.com/u/user89)\
**Post date:** [September 28, 2019, 4:50am UTC](https://forum.ansible.com/t/can-inventory-plugin-constructed-create-a-groups-based-on-ansible-facts/31047/4 "2019-09-28T04:50:04Z")

</div>

Thank you guys, it was done by enabling facts caching. It was disabled that is why didn't work.
