VMware: new module : vmware_local_user_facts (#37167)

Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
pull/39787/head
Abhijeet Kasurde 7 years ago committed by ansibot
parent 02f1d263c7
commit 693065da1e

@ -0,0 +1,111 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright: (c) 2018, Abhijeet Kasurde <akasurde@redhat.com>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
from __future__ import absolute_import, division, print_function
__metaclass__ = type
ANSIBLE_METADATA = {
'metadata_version': '1.1',
'status': ['preview'],
'supported_by': 'community'
}
DOCUMENTATION = '''
---
module: vmware_local_user_facts
short_description: Gather facts about users on the given ESXi host
description:
- This module can be used to gather facts about users present on the given ESXi host system in VMware infrastructure.
- All variables and VMware object names are case sensitive.
- User must hold the 'Authorization.ModifyPermissions' privilege to invoke this module.
version_added: "2.6"
author:
- Abhijeet Kasurde (@akasurde) <akasurde@redhat.com>
notes:
- Tested on ESXi 6.5
requirements:
- "python >= 2.6"
- PyVmomi
extends_documentation_fragment: vmware.documentation
'''
EXAMPLES = r'''
- name: Gather facts about all Users on given ESXi host system
vmware_local_user_facts:
hostname: esxi_hostname
username: root
password: vmware
register: all_user_facts
'''
RETURN = r'''
local_user_facts:
description: metadata about all local users
returned: always
type: dict
sample: [
{
"full_name": "Administrator",
"principal": "root",
"user_group": false,
"user_id": 0,
"user_shell_access": true
},
{
"full_name": "DCUI User",
"principal": "dcui",
"user_group": false,
"user_id": 100,
"user_shell_access": false
},
]
'''
try:
from pyVmomi import vim, vmodl
except ImportError:
pass
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.vmware import PyVmomi, vmware_argument_spec
class VMwareUserFactsManager(PyVmomi):
def __init__(self, module):
super(VMwareUserFactsManager, self).__init__(module)
if self.is_vcenter():
self.module.fail_json(msg="Failed to get local authorization manager settings.",
details="It seems that %s is a vCenter server "
"instead of an ESXi server." % self.params['hostname'])
def gather_user_facts(self):
"""
Function to gather facts about local users
"""
results = dict(changed=False, local_user_facts=[])
user_account = self.content.userDirectory.RetrieveUserGroups(None, '', None, None, False, True, False)
if user_account:
for user in user_account:
temp_user = dict(principal=user.principal,
full_name=user.fullName,
user_group=user.group,
user_id=user.id,
user_shell_access=user.shellAccess)
results['local_user_facts'].append(temp_user)
self.module.exit_json(**results)
def main():
argument_spec = vmware_argument_spec()
module = AnsibleModule(argument_spec=argument_spec)
vmware_local_user_facts = VMwareUserFactsManager(module)
vmware_local_user_facts.gather_user_facts()
if __name__ == '__main__':
main()

@ -0,0 +1,3 @@
posix/ci/cloud/group1/vcenter
cloud/vcenter
destructive

@ -0,0 +1,57 @@
# Test code for the vmware_local_user_facts module.
# Copyright: (c) 2018, Abhijeet Kasurde <akasurde@redhat.com>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
# Commenting local user testcases as older vcsim docker image
# does not support this.
#- name: make sure pyvmomi is installed
# pip:
# name: pyvmomi
# state: latest
# when: "{{ ansible_user_id == 'root' }}"
#- name: store the vcenter container ip
# set_fact:
# vcsim: "{{ lookup('env', 'vcenter_host') }}"
#- debug: var=vcsim
#- name: Wait for Flask controller to come up online
# wait_for:
# host: "{{ vcsim }}"
# port: 5000
# state: started
#- name: kill vcsim
# uri:
# url: http://{{ vcsim }}:5000/killall
# Local user manager works only with standalone ESXi server
#- name: start vcsim
# uri:
# url: http://{{ vcsim }}:5000/spawn?esx=1
# register: vcsim_instance
#- debug:
# var: vcsim_instance
#- name: Wait for vcsim server to come up online
# wait_for:
# host: "{{ vcsim }}"
# port: 443
# state: started
#- name: Gather facts about users
# vmware_local_user_facts:
# hostname: "{{ vcsim }}"
# username: "{{ vcsim_instance.json.username }}"
# password: "{{ vcsim_instance.json.password }}"
# validate_certs: False
# register: all_user_facts
#- name: ensure user facts are gathered
# assert:
# that:
# - not all_user_facts.changed
# - all_user_facts.local_user_facts is defined
Loading…
Cancel
Save