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

31 views
Skip to first unread message

Михаил Политаев

unread,
Aug 15, 2019, 9:53:08 AM8/15/19
to Ansible Project
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?

Gert van den Berg

unread,
Sep 18, 2019, 5:00:02 AM9/18/19
to Ansible Project
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 }}

Brian Coca

unread,
Sep 18, 2019, 4:45:01 PM9/18/19
to Ansible Project
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

--
----------
Brian Coca

Михаил Политаев

unread,
Sep 28, 2019, 12:50:05 AM9/28/19
to Ansible Project
Thank you guys, it was done by enabling facts caching. It was disabled that is why didn't work.

Reply all
Reply to author
Forward
0 new messages