From d0bb74a03b79b968a2ba6c3867e3f071dcbfbcc4 Mon Sep 17 00:00:00 2001 From: Ajpantuso Date: Thu, 11 Mar 2021 16:08:11 -0500 Subject: [PATCH] Allow tags strings containing commas in proxmox inventory plug-in (#1949) * Included explicit parsing for proxmox guest tags and updated corresponding unit test with tags key * Including changelog fragment for PR 1949 * Removed ellipsis from test Proxmox only permits periods when surrounded by alphanumeric characters * Corrected punctuation for changelog entry Co-authored-by: Felix Fontein * Allowing tags string to contain commas * Incorporated new parsed tags fact with bugfix * Correcting whitespace issues * Update changelogs/fragments/1949-proxmox-inventory-tags.yml Co-authored-by: Felix Fontein * Update plugins/inventory/proxmox.py Co-authored-by: Felix Fontein * Update changelogs/fragments/1949-proxmox-inventory-tags.yml Co-authored-by: Felix Fontein Co-authored-by: Felix Fontein --- changelogs/fragments/1949-proxmox-inventory-tags.yml | 5 +++++ plugins/inventory/proxmox.py | 10 ++++++++++ tests/unit/plugins/inventory/test_proxmox.py | 6 ++++-- 3 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 changelogs/fragments/1949-proxmox-inventory-tags.yml diff --git a/changelogs/fragments/1949-proxmox-inventory-tags.yml b/changelogs/fragments/1949-proxmox-inventory-tags.yml new file mode 100644 index 0000000000..073428c2e6 --- /dev/null +++ b/changelogs/fragments/1949-proxmox-inventory-tags.yml @@ -0,0 +1,5 @@ +--- +bugfixes: +- proxmox inventory plugin - allowed proxomox tag string to contain commas when returned as fact (https://github.com/ansible-collections/community.general/pull/1949). +minor_changes: +- proxmox inventory plugin - added ``tags_parsed`` fact containing tags parsed as a list (https://github.com/ansible-collections/community.general/pull/1949). diff --git a/plugins/inventory/proxmox.py b/plugins/inventory/proxmox.py index d21688c4d4..bc79d596cb 100644 --- a/plugins/inventory/proxmox.py +++ b/plugins/inventory/proxmox.py @@ -217,6 +217,10 @@ class InventoryModule(BaseInventoryPlugin, Cacheable): vmtype_key = self.to_safe('%s%s' % (self.get_option('facts_prefix'), vmtype_key.lower())) self.inventory.set_variable(name, vmtype_key, vmtype) + plaintext_configs = [ + 'tags', + ] + for config in ret: key = config key = self.to_safe('%s%s' % (self.get_option('facts_prefix'), key.lower())) @@ -226,6 +230,12 @@ class InventoryModule(BaseInventoryPlugin, Cacheable): if config == 'rootfs' or config.startswith(('virtio', 'sata', 'ide', 'scsi')): value = ('disk_image=' + value) + # Additional field containing parsed tags as list + if config == 'tags': + parsed_key = self.to_safe('%s%s' % (key, "_parsed")) + parsed_value = [tag.strip() for tag in value.split(",")] + self.inventory.set_variable(name, parsed_key, parsed_value) + if not (isinstance(value, int) or ',' not in value): # split off strings with commas to a dict # skip over any keys that cannot be processed diff --git a/tests/unit/plugins/inventory/test_proxmox.py b/tests/unit/plugins/inventory/test_proxmox.py index 14332e750e..036c8e5938 100644 --- a/tests/unit/plugins/inventory/test_proxmox.py +++ b/tests/unit/plugins/inventory/test_proxmox.py @@ -71,7 +71,8 @@ def get_json(url): "status": "running", "vmid": "100", "disk": "1000", - "uptime": 1000}] + "uptime": 1000, + "tags": "test, tags, here"}] elif url == "https://localhost:8006/api2/json/nodes/testnode/qemu": # _get_qemu_per_node return [{"name": "test-qemu", @@ -105,7 +106,8 @@ def get_json(url): "vmid": "9001", "uptime": 0, "disk": 0, - "status": "stopped"}] + "status": "stopped", + "tags": "test, tags, here"}] elif url == "https://localhost:8006/api2/json/pools/test": # _get_members_per_pool return {"members": [{"uptime": 1000,