From fad4e2451bd39e4c3d39199eb6e7c4c20404e807 Mon Sep 17 00:00:00 2001 From: Alain Igban Date: Sun, 26 Feb 2023 00:13:28 +0400 Subject: [PATCH 1/2] Added feature to create groups based on tags - split_tags function added to create a list of tags from proxmox_tags variable declared in inventory --- proxmox.py | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/proxmox.py b/proxmox.py index d37fbaf..19675f0 100755 --- a/proxmox.py +++ b/proxmox.py @@ -415,6 +415,21 @@ def main_list(options, config_path): } results[osid]['hosts'] += [vm] + # Create group 'based on proxmox_tags' + # so you can: --limit 'worker,external-datastore' + try: + tags = results['_meta']['hostvars'][vm]['proxmox_tags'] + vm_name = results['_meta']['hostvars'][vm]['proxmox_name'] + tag_list = split_tags(tags) + for i in range(len(tag_list)): + if tag_list[i] not in results: + results[tag_list[i]] = { + 'hosts': [] + } + results[tag_list[i]]['hosts'] += [vm] + except KeyError: + pass + results['_meta']['hostvars'][vm].update(metadata) # pools @@ -422,9 +437,14 @@ def main_list(options, config_path): results[pool] = { 'hosts': proxmox_api.pool(pool).get_members_name(), } - return results +def split_tags(proxmox_tags: str) -> list[str]: + """ + Splits proxmox_tags delimited by comma and returns a list of the tags. + """ + tags = proxmox_tags.split(',') + return tags def main_host(options, config_path): proxmox_api = ProxmoxAPI(options, config_path) @@ -479,7 +499,7 @@ def main(): indent = None if options.pretty: indent = 2 - +#TODO print((json.dumps(data, indent=indent))) From aacf7b119706d436b0593e78e2706447016448b6 Mon Sep 17 00:00:00 2001 From: Alain Igban Date: Sun, 26 Feb 2023 01:40:40 +0400 Subject: [PATCH 2/2] semi-colon delimiter works (not comma) --- proxmox.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/proxmox.py b/proxmox.py index 19675f0..0cd05f3 100755 --- a/proxmox.py +++ b/proxmox.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # Copyright (C) 2014 Mathieu GAUTHIER-LAFAYE # @@ -443,7 +443,7 @@ def split_tags(proxmox_tags: str) -> list[str]: """ Splits proxmox_tags delimited by comma and returns a list of the tags. """ - tags = proxmox_tags.split(',') + tags = proxmox_tags.split(';') return tags def main_host(options, config_path):