VMware: add new module vmware_vmkernel_facts ()

Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
This commit is contained in:
Abhijeet Kasurde 2018-02-05 23:50:48 +05:30 committed by GitHub
parent e9e8151c6d
commit c33b02f71a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 279 additions and 0 deletions
lib/ansible/modules/cloud/vmware
test/integration/targets/vmware_vmkernel_facts

View file

@ -0,0 +1,210 @@
#!/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 = r'''
---
module: vmware_vmkernel_facts
short_description: Gathers VMKernel facts about an ESXi host
description:
- This module can be used to gather VMKernel facts about an ESXi host from given ESXi hostname or cluster name.
version_added: '2.5'
author:
- Abhijeet Kasurde (@akasurde)
notes:
- Tested on vSphere 6.5
requirements:
- python >= 2.6
- PyVmomi
options:
cluster_name:
description:
- Name of the cluster.
- VMKernel facts about each ESXi server will be returned for the given cluster.
- If C(esxi_hostname) is not given, this parameter is required.
esxi_hostname:
description:
- ESXi hostname.
- VMKernel facts about this ESXi server will be returned.
- If C(cluster_name) is not given, this parameter is required.
extends_documentation_fragment: vmware.documentation
'''
EXAMPLES = r'''
- name: Gather VMKernel facts about all ESXi Host in given Cluster
vmware_vmkernel_facts:
hostname: '{{ vcenter_hostname }}'
username: '{{ vcenter_username }}'
password: '{{ vcenter_password }}'
cluster_name: cluster_name
register: cluster_host_vmks
- name: Gather VMKernel facts about ESXi Host
vmware_vmkernel_facts:
hostname: '{{ vcenter_hostname }}'
username: '{{ vcenter_username }}'
password: '{{ vcenter_password }}'
esxi_hostname: '{{ esxi_hostname }}'
register: host_vmks
'''
RETURN = r'''
host_vmk_facts:
description: metadata about VMKernel present on given host system
returned: success
type: dict
sample:
{
"10.76.33.208": [
{
"device": "vmk0",
"dhcp": true,
"enable_ft": false,
"enable_management": true,
"enable_vmotion": false,
"enable_vsan": false,
"ipv4_address": "10.76.33.28",
"ipv4_subnet_mask": "255.255.255.0",
"key": "key-vim.host.VirtualNic-vmk0",
"mac": "52:54:00:12:50:ce",
"mtu": 1500,
"portgroup": "Management Network",
"stack": "defaultTcpipStack"
},
]
}
'''
try:
from pyVmomi import vim, vmodl
except ImportError:
pass
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.vmware import vmware_argument_spec, PyVmomi
from ansible.module_utils._text import to_native
class VmkernelFactsManager(PyVmomi):
def __init__(self, module):
super(VmkernelFactsManager, self).__init__(module)
cluster_name = self.params.get('cluster_name', None)
esxi_host_name = self.params.get('esxi_hostname', None)
self.hosts = []
if cluster_name:
cluster_obj = self.find_cluster_by_name(cluster_name=cluster_name)
if cluster_obj:
self.hosts = [host for host in cluster_obj.host]
else:
module.fail_json(changed=False, msg="Cluster '%s' not found" % cluster_name)
elif esxi_host_name:
esxi_host_obj = self.find_hostsystem_by_name(host_name=esxi_host_name)
if esxi_host_obj:
self.hosts = [esxi_host_obj]
else:
module.fail_json(changed=False, msg="ESXi '%s' not found" % esxi_host_name)
self.service_type_vmks = dict()
self.get_all_vmks_by_service_type()
def get_all_vmks_by_service_type(self):
"""
Function to return information about service types and VMKernel
"""
for host in self.hosts:
self.service_type_vmks[host.name] = dict(vmotion=[], vsan=[], management=[], faultToleranceLogging=[])
for service_type in self.service_type_vmks[host.name].keys():
vmks_list = self.query_service_type_for_vmks(host, service_type)
self.service_type_vmks[host.name][service_type] = vmks_list
def query_service_type_for_vmks(self, host_system, service_type):
"""
Function to return list of VMKernels
Args:
host_system: Host system managed object
service_type: Name of service type
Returns: List of VMKernel which belongs to that service type
"""
vmks_list = []
query = None
try:
query = host_system.configManager.virtualNicManager.QueryNetConfig(service_type)
except vim.fault.HostConfigFault as config_fault:
self.module.fail_json(msg="Failed to get all VMKs for service type %s due to"
" host config fault : %s" % (service_type, to_native(config_fault.msg)))
except vmodl.fault.InvalidArgument as invalid_argument:
self.module.fail_json(msg="Failed to get all VMKs for service type %s due to"
" invalid arguments : %s" % (service_type, to_native(invalid_argument.msg)))
except Exception as e:
self.module.fail_json(msg="Failed to get all VMKs for service type %s due to"
"%s" % (service_type, to_native(e)))
if not query.selectedVnic:
return vmks_list
selected_vnics = [vnic for vnic in query.selectedVnic]
vnics_with_service_type = [vnic.device for vnic in query.candidateVnic if vnic.key in selected_vnics]
return vnics_with_service_type
def gather_host_vmk_facts(self):
hosts_facts = {}
for host in self.hosts:
host_vmk_facts = []
host_network_system = host.config.network
if host_network_system:
vmks_config = host.config.network.vnic
for vmk in vmks_config:
host_vmk_facts.append(dict(
device=vmk.device,
key=vmk.key,
portgroup=vmk.portgroup,
ipv4_address=vmk.spec.ip.ipAddress,
ipv4_subnet_mask=vmk.spec.ip.subnetMask,
dhcp=vmk.spec.ip.dhcp,
mac=vmk.spec.mac,
mtu=vmk.spec.mtu,
stack=vmk.spec.netStackInstanceKey,
enable_vsan=vmk.device in self.service_type_vmks[host.name]['vsan'],
enable_vmotion=vmk.device in self.service_type_vmks[host.name]['vmotion'],
enable_management=vmk.device in self.service_type_vmks[host.name]['management'],
enable_ft=vmk.device in self.service_type_vmks[host.name]['faultToleranceLogging'],
)
)
hosts_facts[host.name] = host_vmk_facts
return hosts_facts
def main():
argument_spec = vmware_argument_spec()
argument_spec.update(
cluster_name=dict(type='str', required=False),
esxi_hostname=dict(type='str', required=False),
)
module = AnsibleModule(
argument_spec=argument_spec,
required_one_of=[
['cluster_name', 'esxi_hostname'],
]
)
vmware_vmk_config = VmkernelFactsManager(module)
module.exit_json(changed=False, host_vmk_facts=vmware_vmk_config.gather_host_vmk_facts())
if __name__ == "__main__":
main()

View file

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

View file

@ -0,0 +1,66 @@
# Test code for the vmware_vmkernel_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)
# TODO: vcsim does not support HostVirtualNicManager related to operations
#- 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
#- name: start vcsim
# uri:
# url: http://{{ vcsim }}:5000/spawn?cluster=2
# 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: get a list of hosts from vcsim
# uri:
# url: http://{{ vcsim }}:5000/govc_find?filter=H
# register: hosts
#- name: get a host
# set_fact:
# host1: "{{ hosts.json[0] | basename }}"
#- debug: var=host1
#- name: Gather VNICs facts about all hosts in given cluster
# vmware_vmkernel_facts:
# hostname: "{{ vcsim }}"
# username: "{{ user }}"
# password: "{{ passwd }}"
# esxi_hostname: "{{ host1 }}"
# validate_certs: no
# register: host_vmkernel
#- debug: var=host_vmkernel
#- assert:
# that:
# - host_vmkernel.host_vmk_facts is defined