Can inventory plugin "constructed" create a groups based on ansible_facts

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?

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

  • group_by:
    key: distro_{{ ansible_distribution }}

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

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