Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vmware_object_role_permission: Parameter object_name also accepts paths #2315

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
33 changes: 29 additions & 4 deletions plugins/modules/vmware_object_role_permission.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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'''
Expand All @@ -144,7 +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):
Expand Down Expand Up @@ -282,9 +297,19 @@ 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(
Expand Down