| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2018, Abhijeet Kasurde <[email protected]> |
| 5 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 7 |
|
| 8 |
from __future__ import absolute_import, division, print_function |
| 9 |
__metaclass__ = type |
| 10 |
|
| 11 |
|
| 12 |
DOCUMENTATION = r''' |
| 13 |
--- |
| 14 |
module: vmware_local_role_info |
| 15 |
short_description: Gather info about local roles on an ESXi host or vCenter |
| 16 |
description: |
| 17 |
- This module can be used to gather information about local role info on an ESXi host or vCenter |
| 18 |
author: |
| 19 |
- Abhijeet Kasurde (@Akasurde) |
| 20 |
notes: |
| 21 |
- Be sure that the user used for login, has the appropriate rights to view roles |
| 22 |
- The module returns a list of dict in version 2.8 and above. |
| 23 |
extends_documentation_fragment: |
| 24 |
- vmware.vmware.base_options |
| 25 |
|
| 26 |
''' |
| 27 |
|
| 28 |
EXAMPLES = r''' |
| 29 |
- name: Gather info about local role from an ESXi (or vCenter) |
| 30 |
community.vmware.vmware_local_role_info: |
| 31 |
hostname: '{{ esxi_hostname }}' |
| 32 |
username: '{{ esxi_username }}' |
| 33 |
password: '{{ esxi_password }}' |
| 34 |
register: fact_details |
| 35 |
delegate_to: localhost |
| 36 |
- name: Get Admin privileges |
| 37 |
set_fact: |
| 38 |
admin_priv: "{{ fact_details.local_role_info | selectattr('role_name', 'equalto', 'Admin') | map(attribute='privileges') | first }}" |
| 39 |
- debug: |
| 40 |
msg: "{{ admin_priv }}" |
| 41 |
''' |
| 42 |
|
| 43 |
RETURN = r''' |
| 44 |
local_role_info: |
| 45 |
description: A list of dict about role information present on ESXi host |
| 46 |
returned: always |
| 47 |
type: list |
| 48 |
sample: [ |
| 49 |
{ |
| 50 |
"privileges": [ |
| 51 |
"Alarm.Acknowledge", |
| 52 |
"Alarm.Create", |
| 53 |
"Alarm.Delete", |
| 54 |
"Alarm.DisableActions", |
| 55 |
], |
| 56 |
"role_id": -12, |
| 57 |
"role_info_label": "Ansible User", |
| 58 |
"role_info_summary": "Ansible Automation user", |
| 59 |
"role_name": "AnsiUser1", |
| 60 |
"role_system": true |
| 61 |
}, |
| 62 |
{ |
| 63 |
"privileges": [], |
| 64 |
"role_id": -5, |
| 65 |
"role_info_label": "No access", |
| 66 |
"role_info_summary": "Used for restricting granted access", |
| 67 |
"role_name": "NoAccess", |
| 68 |
"role_system": true |
| 69 |
}, |
| 70 |
{ |
| 71 |
"privileges": [ |
| 72 |
"System.Anonymous", |
| 73 |
"System.View" |
| 74 |
], |
| 75 |
"role_id": -3, |
| 76 |
"role_info_label": "View", |
| 77 |
"role_info_summary": "Visibility access (cannot be granted)", |
| 78 |
"role_name": "View", |
| 79 |
"role_system": true |
| 80 |
} |
| 81 |
] |
| 82 |
''' |
| 83 |
|
| 84 |
from ansible.module_utils.basic import AnsibleModule |
| 85 |
from ansible_collections.community.vmware.plugins.module_utils.vmware import PyVmomi |
| 86 |
from ansible_collections.vmware.vmware.plugins.module_utils.argument_spec import base_argument_spec |
| 87 |
|
| 88 |
|
| 89 |
class VMwareLocalRoleInfo(PyVmomi): |
| 90 |
"""Class to manage local role info""" |
| 91 |
|
| 92 |
def __init__(self, module): |
| 93 |
super(VMwareLocalRoleInfo, self).__init__(module) |
| 94 |
self.module = module |
| 95 |
self.params = module.params |
| 96 |
|
| 97 |
if self.content.authorizationManager is None: |
| 98 |
self.module.fail_json( |
| 99 |
msg="Failed to get local authorization manager settings.", |
| 100 |
details="It seems that '%s' does not support this functionality" % self.params['hostname'] |
| 101 |
) |
| 102 |
|
| 103 |
def gather_local_role_info(self): |
| 104 |
"""Gather info about local roles""" |
| 105 |
results = list() |
| 106 |
for role in self.content.authorizationManager.roleList: |
| 107 |
results.append( |
| 108 |
dict( |
| 109 |
role_name=role.name, |
| 110 |
role_id=role.roleId, |
| 111 |
privileges=list(role.privilege), |
| 112 |
role_system=role.system, |
| 113 |
role_info_label=role.info.label, |
| 114 |
role_info_summary=role.info.summary, |
| 115 |
) |
| 116 |
) |
| 117 |
|
| 118 |
self.module.exit_json(changed=False, local_role_info=results) |
| 119 |
|
| 120 |
|
| 121 |
def main(): |
| 122 |
"""Main""" |
| 123 |
argument_spec = base_argument_spec() |
| 124 |
module = AnsibleModule(argument_spec=argument_spec, |
| 125 |
supports_check_mode=True) |
| 126 |
|
| 127 |
vmware_local_role_info = VMwareLocalRoleInfo(module) |
| 128 |
vmware_local_role_info.gather_local_role_info() |
| 129 |
|
| 130 |
|
| 131 |
if __name__ == '__main__': |
| 132 |
main() |