ansible-collections / hetzner.hcloud

A collection to manage resources on Hetzner Cloud
https://galaxy.ansible.com/ui/repo/published/hetzner/hcloud
GNU General Public License v3.0
108 stars 38 forks source link

[inventory-plugin] allow (dynamic) grouping by multiple keys #583

Open c33s opened 1 week ago

c33s commented 1 week ago
SUMMARY

it would be quite handy to allow to group servers by multiple keys. especially handy for child-groups.

ISSUE TYPE
COMPONENT NAME

inventory plugin

ADDITIONAL INFORMATION

it would simply help to create more complex groups.

plugin: hetzner.hcloud.hcloud
keyed_groups:
  # syntax like https://docs.hetzner.cloud/#label-selector
  - key: [location=ngb1, image_os_flavor=debian, label.role=app, label.app_version=v1, image_id notin (123, 456, 789 )]
    parent_group: app
    prefix: legacy_app

would create group legacy_app for all servers which are in the location ngb1, are debian based, have the label role with the key app and where the image ids are not in the ones specified.

the dynamic functionality (matrix) could also be split into its own feature request, for now i put it here.

plugin: hetzner.hcloud.hcloud
keyed_groups:
  - key: [server_type, label.role]

for the example we assume we have server types cx11 and ccx23 and the roles app, ingres and db would create:

server_type_cx11_app
server_type_cx11_ingres
server_type_cx11_db
server_type_ccx23_app
server_type_ccx23_ingres
server_type_ccx23_db

nice would also be to allow to group even more complex.

syntax is far from optimal and thought through. it is just for descriping the feature in some way. maybe have a look at gitlab ci matrix syntax

plugin: hetzner.hcloud.hcloud
keyed_groups:
  - key: 
    -  server_type: 
        - in ccx13 ccx23 ccx33 ccx43 ccx53 ccx63 as cpu_dedicated 
        - in cx22 cx32 cx42 cx52 as cpu_shared
    - label.role

creates:

cpu_dedicated_app
cpu_dedicated_ingres
cpu_dedicated_db
cpu_shared_app
cpu_shared_ingres
cpu_shared_db
jooola commented 11 hours ago

The keyed_groups, groups and compose inventory options are part of an upstream extension to the inventory plugin.

I recommend you to open this issue upstream at https://github.com/ansible/ansible https://docs.ansible.com/ansible/latest/dev_guide/developing_inventory.html#constructed-features

That said, you might be able to do this using the additional groups and compose options: https://docs.ansible.com/ansible/latest/plugins/inventory.html#using-inventory-plugins