From e4b210eab2e7fa8e26791c4e2c8b3e02984ccffb Mon Sep 17 00:00:00 2001 From: Guillaume Vandenhove Date: Tue, 20 Dec 2022 11:56:50 +0100 Subject: [PATCH 1/7] feat: add tags to proxmox containers --- plugins/modules/proxmox.py | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/plugins/modules/proxmox.py b/plugins/modules/proxmox.py index 89706655f26..dbe7501061f 100644 --- a/plugins/modules/proxmox.py +++ b/plugins/modules/proxmox.py @@ -106,6 +106,14 @@ description: - sets DNS search domain for a container type: str + tags: + description: + - List of tags to apply to the container. + - Tags must start with C([a-z0-9_]) followed by zero or more of the following characters C([a-z0-9_-+.]). + - Tags are only available in Proxmox 7+. + type: list + elements: str + version_added: TODO timeout: description: - timeout for operations @@ -391,6 +399,7 @@ state: absent ''' +import re import time from ansible_collections.community.general.plugins.module_utils.version import LooseVersion @@ -415,11 +424,23 @@ def is_template_container(self, node, vmid): return config['template'] def create_instance(self, vmid, node, disk, storage, cpus, memory, swap, timeout, clone, **kwargs): + # Available only in PVE 7 + only_v7 = ['tags'] + proxmox_node = self.proxmox_api.nodes(node) # Remove all empty kwarg entries kwargs = dict((k, v) for k, v in kwargs.items() if v is not None) + version = self.version() + pve_major_version = 3 if version < LooseVersion('4.0') else version.version[0] + + #The features work only on PVE 7 + if pve_major_version < 7: + for p in only_v7: + if p in kwargs: + del kwargs[p] + if VZ_TYPE == 'lxc': kwargs['cpulimit'] = cpus kwargs['rootfs'] = disk @@ -437,6 +458,14 @@ def create_instance(self, vmid, node, disk, storage, cpus, memory, swap, timeout kwargs['cpus'] = cpus kwargs['disk'] = disk + # LXC tags are expected to be valid and presented as a comma/semi-colon delimited string + if 'tags' in kwargs: + re_tag = re.compile(r'^[a-z0-9_][a-z0-9_\-\+\.]*$') + for tag in kwargs['tags']: + if not re_tag.match(tag): + self.module.fail_json(msg='%s is not a valid tag' % tag) + kwargs['tags'] = ",".join(kwargs['tags']) + if clone is not None: if VZ_TYPE != 'lxc': self.module.fail_json(changed=False, msg="Clone operator is only supported for LXC enabled proxmox clusters.") @@ -569,6 +598,7 @@ def main(): proxmox_default_behavior=dict(type='str', default='no_defaults', choices=['compatibility', 'no_defaults']), clone=dict(type='int'), clone_type=dict(default='opportunistic', choices=['full', 'linked', 'opportunistic']), + tags=dict(type='list', elements='str') ) module_args.update(proxmox_args) @@ -674,7 +704,8 @@ def main(): features=",".join(module.params['features']) if module.params['features'] is not None else None, unprivileged=ansible_to_proxmox_bool(module.params['unprivileged']), description=module.params['description'], - hookscript=module.params['hookscript']) + hookscript=module.params['hookscript'], + tags=module.params['tags']) module.exit_json(changed=True, msg="Deployed VM %s from template %s" % (vmid, module.params['ostemplate'])) except Exception as e: From ad6c413efdb43df0bc61cadfe49e962a618e9573 Mon Sep 17 00:00:00 2001 From: Guillaume Vandenhove Date: Tue, 20 Dec 2022 12:12:36 +0100 Subject: [PATCH 2/7] fix: correct version added --- plugins/modules/proxmox.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/modules/proxmox.py b/plugins/modules/proxmox.py index dbe7501061f..5331a7dd94c 100644 --- a/plugins/modules/proxmox.py +++ b/plugins/modules/proxmox.py @@ -113,7 +113,7 @@ - Tags are only available in Proxmox 7+. type: list elements: str - version_added: TODO + version_added: 6.3.0 timeout: description: - timeout for operations From ad4cb8c57b177721f100027e65743871720924d9 Mon Sep 17 00:00:00 2001 From: Guillaume Vandenhove Date: Tue, 20 Dec 2022 12:13:59 +0100 Subject: [PATCH 3/7] fix: code style --- plugins/modules/proxmox.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/plugins/modules/proxmox.py b/plugins/modules/proxmox.py index 5331a7dd94c..5dbe157d3e8 100644 --- a/plugins/modules/proxmox.py +++ b/plugins/modules/proxmox.py @@ -435,11 +435,11 @@ def create_instance(self, vmid, node, disk, storage, cpus, memory, swap, timeout version = self.version() pve_major_version = 3 if version < LooseVersion('4.0') else version.version[0] - #The features work only on PVE 7 + # The features work only on PVE 7 if pve_major_version < 7: - for p in only_v7: - if p in kwargs: - del kwargs[p] + for p in only_v7: + if p in kwargs: + del kwargs[p] if VZ_TYPE == 'lxc': kwargs['cpulimit'] = cpus From 685634cb60beeb1bc2bbbbb0b074d975c3130bd5 Mon Sep 17 00:00:00 2001 From: Guillaume Vandenhove Date: Tue, 20 Dec 2022 12:36:22 +0100 Subject: [PATCH 4/7] feat: changelog fragment --- changelogs/fragments/5714-proxmox-lxc-tag-support.yml | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 changelogs/fragments/5714-proxmox-lxc-tag-support.yml diff --git a/changelogs/fragments/5714-proxmox-lxc-tag-support.yml b/changelogs/fragments/5714-proxmox-lxc-tag-support.yml new file mode 100644 index 00000000000..5e3dcc8fcf3 --- /dev/null +++ b/changelogs/fragments/5714-proxmox-lxc-tag-support.yml @@ -0,0 +1,3 @@ +--- +minor_changes: +- proxmox - added new module parameter ``tags`` for use with PVE 7+ (https://github.com/ansible-collections/community.general/pull/5714). From cd27a96dab56c7a7cc6976622add3a460fc36973 Mon Sep 17 00:00:00 2001 From: GuillaumeV-cemea <101114641+GuillaumeV-cemea@users.noreply.github.com> Date: Tue, 20 Dec 2022 14:02:29 +0100 Subject: [PATCH 5/7] fix: correct version_added Co-authored-by: Felix Fontein --- plugins/modules/proxmox.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/modules/proxmox.py b/plugins/modules/proxmox.py index 5dbe157d3e8..739f68d9496 100644 --- a/plugins/modules/proxmox.py +++ b/plugins/modules/proxmox.py @@ -113,7 +113,7 @@ - Tags are only available in Proxmox 7+. type: list elements: str - version_added: 6.3.0 + version_added: 6.2.0 timeout: description: - timeout for operations From 2299a3ec89e218969a599f52308942ed71c04657 Mon Sep 17 00:00:00 2001 From: Guillaume Vandenhove Date: Wed, 21 Dec 2022 09:45:53 +0100 Subject: [PATCH 6/7] feat: fail on unsupported params, rather than silently ignoring them --- plugins/modules/proxmox.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/plugins/modules/proxmox.py b/plugins/modules/proxmox.py index 739f68d9496..3a21647f872 100644 --- a/plugins/modules/proxmox.py +++ b/plugins/modules/proxmox.py @@ -424,9 +424,11 @@ def is_template_container(self, node, vmid): return config['template'] def create_instance(self, vmid, node, disk, storage, cpus, memory, swap, timeout, clone, **kwargs): - # Available only in PVE 7 - only_v7 = ['tags'] + # Version limited features + minimum_version = { + 'tags': 7, + } proxmox_node = self.proxmox_api.nodes(node) # Remove all empty kwarg entries @@ -435,11 +437,11 @@ def create_instance(self, vmid, node, disk, storage, cpus, memory, swap, timeout version = self.version() pve_major_version = 3 if version < LooseVersion('4.0') else version.version[0] - # The features work only on PVE 7 - if pve_major_version < 7: - for p in only_v7: - if p in kwargs: - del kwargs[p] + # Fail on unsupported features + for option, version in minimum_version.items(): + if pve_major_version < version: + self.module.fail_json(changed=False, msg="Feature {option} is only supported in PVE {version}+, and you're using PVE {pve_major_version}". + format(option=option, version=version, pve_major_version=pve_major_version)) if VZ_TYPE == 'lxc': kwargs['cpulimit'] = cpus From 854b4836101d6eba423a14f164e8a6b58c56d14d Mon Sep 17 00:00:00 2001 From: Guillaume Vandenhove Date: Thu, 22 Dec 2022 09:25:06 +0100 Subject: [PATCH 7/7] fix: actually check unsupported feature presence before failing --- plugins/modules/proxmox.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/modules/proxmox.py b/plugins/modules/proxmox.py index 3a21647f872..640f9b41142 100644 --- a/plugins/modules/proxmox.py +++ b/plugins/modules/proxmox.py @@ -439,7 +439,7 @@ def create_instance(self, vmid, node, disk, storage, cpus, memory, swap, timeout # Fail on unsupported features for option, version in minimum_version.items(): - if pve_major_version < version: + if pve_major_version < version and option in kwargs: self.module.fail_json(changed=False, msg="Feature {option} is only supported in PVE {version}+, and you're using PVE {pve_major_version}". format(option=option, version=version, pve_major_version=pve_major_version))