From 5c8000ad607111d00f4c864daafefe048f750c71 Mon Sep 17 00:00:00 2001 From: --show-origin Date: Thu, 13 Feb 2025 08:21:27 +0100 Subject: [PATCH 1/5] vmware_object_role_permission: Parameter object_name also accepts paths --- ...ct_role_permission-path-as-object_name.yml | 2 ++ .../modules/vmware_object_role_permission.py | 35 ++++++++++++++++--- 2 files changed, 32 insertions(+), 5 deletions(-) create mode 100644 changelogs/fragments/vmware_object_role_permission-path-as-object_name.yml diff --git a/changelogs/fragments/vmware_object_role_permission-path-as-object_name.yml b/changelogs/fragments/vmware_object_role_permission-path-as-object_name.yml new file mode 100644 index 0000000000..15f1a62605 --- /dev/null +++ b/changelogs/fragments/vmware_object_role_permission-path-as-object_name.yml @@ -0,0 +1,2 @@ +minor_changes: + - vmware_object_role_permission - Allow passing a path as object_name to identify objects where the name is not unique diff --git a/plugins/modules/vmware_object_role_permission.py b/plugins/modules/vmware_object_role_permission.py index f05a3a5ff5..d976e4464b 100644 --- a/plugins/modules/vmware_object_role_permission.py +++ b/plugins/modules/vmware_object_role_permission.py @@ -43,6 +43,8 @@ object_name: description: - The object name to assigned permission. + - You can also pass the full path to the object if the name is not unique + - A path must include the root-folder for the object-type, see example type: str required: true object_type: @@ -128,6 +130,18 @@ object_name: services state: present delegate_to: localhost + +- name: Assign domain user to VM folder /Test-VMs/Webserver + community.vmware.vmware_object_role_permission: + hostname: "{{ vcenter_hostname }}" + username: "{{ vcenter_username }}" + password: "{{ vcenter_password }}" + validate_certs: false + role: Admin + principal: "vsphere.local\\Test-Webserver-Admin" + object_name: /vm/Test-VMs/Webserver + state: present + delegate_to: localhost ''' RETURN = r''' @@ -144,8 +158,8 @@ from ansible.module_utils.basic import AnsibleModule from ansible.module_utils._text import to_native -from ansible_collections.community.vmware.plugins.module_utils.vmware import PyVmomi, vmware_argument_spec, find_obj - +from ansible_collections.community.vmware.plugins.module_utils.vmware import PyVmomi, vmware_argument_spec, find_obj, compile_folder_path_for_object +import os.path class VMwareObjectRolePermission(PyVmomi): def __init__(self, module): @@ -282,9 +296,20 @@ def get_object(self): getattr(vim, self.params['object_type']) except AttributeError: self.module.fail_json(msg="Object type %s is not valid." % self.params['object_type']) - self.current_obj = find_obj(content=self.content, - vimtype=[getattr(vim, self.params['object_type'])], - name=self.params['object_name']) + + if self.params['object_name'].startswith('/'): + object_path_elements = os.path.split(self.params['object_name']) + all_objects_with_name = find_obj(content=self.content, + vimtype=[getattr(vim, self.params['object_type'])], + name=object_path_elements[1], + first=False) + + found_obj = [obj for obj in all_objects_with_name if self.params['object_name'] == compile_folder_path_for_object(obj)] + self.current_obj = found_obj[0] if found_obj else None + else: + self.current_obj = find_obj(content=self.content, + vimtype=[getattr(vim, self.params['object_type'])], + name=self.params['object_name']) if self.current_obj is None: self.module.fail_json( From 6d28d136cef75b7b6265a0971b4be0c11f6cbfb2 Mon Sep 17 00:00:00 2001 From: --show-origin Date: Thu, 13 Feb 2025 08:31:12 +0100 Subject: [PATCH 2/5] Renamed changelog-fragment to include the PR number --- ...=> 2315-vmware_object_role_permission-path-as-object_name.yml} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename changelogs/fragments/{vmware_object_role_permission-path-as-object_name.yml => 2315-vmware_object_role_permission-path-as-object_name.yml} (100%) diff --git a/changelogs/fragments/vmware_object_role_permission-path-as-object_name.yml b/changelogs/fragments/2315-vmware_object_role_permission-path-as-object_name.yml similarity index 100% rename from changelogs/fragments/vmware_object_role_permission-path-as-object_name.yml rename to changelogs/fragments/2315-vmware_object_role_permission-path-as-object_name.yml From 20c61590268ff5ddf070e9c6953dbcf5f2be6e6e Mon Sep 17 00:00:00 2001 From: --show-origin Date: Thu, 13 Feb 2025 08:51:11 +0100 Subject: [PATCH 3/5] vmware_object_role_permission: Fixed wrong indendation --- plugins/modules/vmware_object_role_permission.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/plugins/modules/vmware_object_role_permission.py b/plugins/modules/vmware_object_role_permission.py index d976e4464b..ba7cd88d3e 100644 --- a/plugins/modules/vmware_object_role_permission.py +++ b/plugins/modules/vmware_object_role_permission.py @@ -300,10 +300,9 @@ def get_object(self): if self.params['object_name'].startswith('/'): object_path_elements = os.path.split(self.params['object_name']) all_objects_with_name = find_obj(content=self.content, - vimtype=[getattr(vim, self.params['object_type'])], - name=object_path_elements[1], - first=False) - + vimtype=[getattr(vim, self.params['object_type'])], + name=object_path_elements[1], + first=False) found_obj = [obj for obj in all_objects_with_name if self.params['object_name'] == compile_folder_path_for_object(obj)] self.current_obj = found_obj[0] if found_obj else None else: From 1eb3930260b0f41e5f90da95688a20f3784c5d96 Mon Sep 17 00:00:00 2001 From: --show-origin Date: Thu, 13 Feb 2025 09:04:27 +0100 Subject: [PATCH 4/5] vmware_object_role_permission: Added empty line required by linter --- plugins/modules/vmware_object_role_permission.py | 1 + 1 file changed, 1 insertion(+) diff --git a/plugins/modules/vmware_object_role_permission.py b/plugins/modules/vmware_object_role_permission.py index ba7cd88d3e..bf0300429c 100644 --- a/plugins/modules/vmware_object_role_permission.py +++ b/plugins/modules/vmware_object_role_permission.py @@ -161,6 +161,7 @@ from ansible_collections.community.vmware.plugins.module_utils.vmware import PyVmomi, vmware_argument_spec, find_obj, compile_folder_path_for_object import os.path + class VMwareObjectRolePermission(PyVmomi): def __init__(self, module): super(VMwareObjectRolePermission, self).__init__(module) From 4f41b47db9b1baed0ae597f5fedcb8301310fc4f Mon Sep 17 00:00:00 2001 From: Fleege <102604977+Fleege@users.noreply.github.com> Date: Thu, 13 Feb 2025 10:37:29 +0100 Subject: [PATCH 5/5] Nothing changed, just want to restart ansible/check as the last failure seems to be related to something outside the code