|
|
@ -1,22 +1,26 @@
|
|
|
|
#!/usr/bin/python
|
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
|
|
|
|
# Copyright: (c) 2017, Wei Gao <gaowei3@qq.com>
|
|
|
|
# Copyright: (c) 2017, Wei Gao <gaowei3@qq.com>
|
|
|
|
|
|
|
|
# Copyright: (c) 2018, Ansible Project
|
|
|
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
# 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
|
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
__metaclass__ = type
|
|
|
|
__metaclass__ = type
|
|
|
|
|
|
|
|
|
|
|
|
ANSIBLE_METADATA = {'metadata_version': '1.1',
|
|
|
|
ANSIBLE_METADATA = {
|
|
|
|
|
|
|
|
'metadata_version': '1.1',
|
|
|
|
'status': ['preview'],
|
|
|
|
'status': ['preview'],
|
|
|
|
'supported_by': 'community'}
|
|
|
|
'supported_by': 'community'
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
---
|
|
|
|
module: vmware_host_facts
|
|
|
|
module: vmware_host_facts
|
|
|
|
short_description: Gathers facts about remote vmware host
|
|
|
|
short_description: Gathers facts about remote ESXi hostsystem
|
|
|
|
description:
|
|
|
|
description:
|
|
|
|
- Gathers facts about remote vmware host.
|
|
|
|
- This module can be used to gathers facts like CPU, memory, datastore, network and system etc. about ESXi host system.
|
|
|
|
|
|
|
|
- Please specify hostname or IP address of ESXi host system as C(hostname).
|
|
|
|
|
|
|
|
- If hostname or IP address of vCenter is provided as C(hostname), then information about first ESXi hostsystem is returned.
|
|
|
|
version_added: 2.5
|
|
|
|
version_added: 2.5
|
|
|
|
author:
|
|
|
|
author:
|
|
|
|
- Wei Gao (@woshihaoren)
|
|
|
|
- Wei Gao (@woshihaoren)
|
|
|
@ -41,40 +45,92 @@ ansible_facts:
|
|
|
|
description: system info about the host machine
|
|
|
|
description: system info about the host machine
|
|
|
|
returned: always
|
|
|
|
returned: always
|
|
|
|
type: dict
|
|
|
|
type: dict
|
|
|
|
|
|
|
|
sample:
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
"ansible_all_ipv4_addresses": [
|
|
|
|
|
|
|
|
"10.76.33.200"
|
|
|
|
|
|
|
|
],
|
|
|
|
|
|
|
|
"ansible_bios_date": "2011-01-01T00:00:00+00:00",
|
|
|
|
|
|
|
|
"ansible_bios_version": "0.5.1",
|
|
|
|
|
|
|
|
"ansible_datastore": [
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
"free": "11.63 GB",
|
|
|
|
|
|
|
|
"name": "datastore1",
|
|
|
|
|
|
|
|
"total": "12.50 GB"
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
],
|
|
|
|
|
|
|
|
"ansible_distribution": "VMware ESXi",
|
|
|
|
|
|
|
|
"ansible_distribution_build": "4887370",
|
|
|
|
|
|
|
|
"ansible_distribution_version": "6.5.0",
|
|
|
|
|
|
|
|
"ansible_hostname": "10.76.33.100",
|
|
|
|
|
|
|
|
"ansible_interfaces": [
|
|
|
|
|
|
|
|
"vmk0"
|
|
|
|
|
|
|
|
],
|
|
|
|
|
|
|
|
"ansible_memfree_mb": 2702,
|
|
|
|
|
|
|
|
"ansible_memtotal_mb": 4095,
|
|
|
|
|
|
|
|
"ansible_os_type": "vmnix-x86",
|
|
|
|
|
|
|
|
"ansible_processor": "Intel Xeon E312xx (Sandy Bridge)",
|
|
|
|
|
|
|
|
"ansible_processor_cores": 2,
|
|
|
|
|
|
|
|
"ansible_processor_count": 2,
|
|
|
|
|
|
|
|
"ansible_processor_vcpus": 2,
|
|
|
|
|
|
|
|
"ansible_product_name": "KVM",
|
|
|
|
|
|
|
|
"ansible_product_serial": "NA",
|
|
|
|
|
|
|
|
"ansible_system_vendor": "Red Hat",
|
|
|
|
|
|
|
|
"ansible_vmk0": {
|
|
|
|
|
|
|
|
"device": "vmk0",
|
|
|
|
|
|
|
|
"ipv4": {
|
|
|
|
|
|
|
|
"address": "10.76.33.100",
|
|
|
|
|
|
|
|
"netmask": "255.255.255.0"
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
"macaddress": "52:54:00:56:7d:59",
|
|
|
|
|
|
|
|
"mtu": 1500
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
'''
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
|
|
from ansible.module_utils.basic import AnsibleModule, bytes_to_human
|
|
|
|
from ansible.module_utils.basic import AnsibleModule, bytes_to_human
|
|
|
|
from ansible.module_utils.vmware import connect_to_api, vmware_argument_spec, find_obj
|
|
|
|
from ansible.module_utils.vmware import PyVmomi, vmware_argument_spec, find_obj
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
from pyVmomi import vim, vmodl
|
|
|
|
from pyVmomi import vim, vmodl
|
|
|
|
HAS_PYVMOMI = True
|
|
|
|
|
|
|
|
except ImportError:
|
|
|
|
except ImportError:
|
|
|
|
HAS_PYVMOMI = False
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_cpu_facts(host):
|
|
|
|
class VMwareHostFactManager(PyVmomi):
|
|
|
|
facts = {
|
|
|
|
def __init__(self, module):
|
|
|
|
'ansible_processor': host.summary.hardware.cpuModel,
|
|
|
|
super(VMwareHostFactManager, self).__init__(module)
|
|
|
|
'ansible_processor_cores': host.summary.hardware.numCpuCores,
|
|
|
|
self.host = find_obj(self.content, [vim.HostSystem], None)
|
|
|
|
'ansible_processor_count': host.summary.hardware.numCpuPkgs,
|
|
|
|
if self.host is None:
|
|
|
|
'ansible_processor_vcpus': host.summary.hardware.numCpuThreads,
|
|
|
|
self.module.fail_json(msg="Failed to find host system.")
|
|
|
|
}
|
|
|
|
|
|
|
|
return facts
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def all_facts(self):
|
|
|
|
def get_memory_facts(host):
|
|
|
|
ansible_facts = {}
|
|
|
|
facts = {
|
|
|
|
ansible_facts.update(self.get_cpu_facts())
|
|
|
|
'ansible_memfree_mb': host.hardware.memorySize // 1024 // 1024 - host.summary.quickStats.overallMemoryUsage,
|
|
|
|
ansible_facts.update(self.get_memory_facts())
|
|
|
|
'ansible_memtotal_mb': host.hardware.memorySize // 1024 // 1024,
|
|
|
|
ansible_facts.update(self.get_datastore_facts())
|
|
|
|
|
|
|
|
ansible_facts.update(self.get_network_facts())
|
|
|
|
|
|
|
|
ansible_facts.update(self.get_system_facts())
|
|
|
|
|
|
|
|
self.module.exit_json(changed=False, ansible_facts=ansible_facts)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_cpu_facts(self):
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
|
|
|
'ansible_processor': self.host.summary.hardware.cpuModel,
|
|
|
|
|
|
|
|
'ansible_processor_cores': self.host.summary.hardware.numCpuCores,
|
|
|
|
|
|
|
|
'ansible_processor_count': self.host.summary.hardware.numCpuPkgs,
|
|
|
|
|
|
|
|
'ansible_processor_vcpus': self.host.summary.hardware.numCpuThreads,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return facts
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_memory_facts(self):
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
|
|
|
'ansible_memfree_mb': self.host.hardware.memorySize // 1024 // 1024 - self.host.summary.quickStats.overallMemoryUsage,
|
|
|
|
|
|
|
|
'ansible_memtotal_mb': self.host.hardware.memorySize // 1024 // 1024,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def get_datastore_facts(host):
|
|
|
|
def get_datastore_facts(self):
|
|
|
|
facts = {}
|
|
|
|
facts = dict()
|
|
|
|
facts['ansible_datastore'] = []
|
|
|
|
facts['ansible_datastore'] = []
|
|
|
|
for store in host.datastore:
|
|
|
|
for store in self.host.datastore:
|
|
|
|
_tmp = {
|
|
|
|
_tmp = {
|
|
|
|
'name': store.summary.name,
|
|
|
|
'name': store.summary.name,
|
|
|
|
'total': bytes_to_human(store.summary.capacity),
|
|
|
|
'total': bytes_to_human(store.summary.capacity),
|
|
|
@ -83,12 +139,11 @@ def get_datastore_facts(host):
|
|
|
|
facts['ansible_datastore'].append(_tmp)
|
|
|
|
facts['ansible_datastore'].append(_tmp)
|
|
|
|
return facts
|
|
|
|
return facts
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_network_facts(self):
|
|
|
|
def get_network_facts(host):
|
|
|
|
facts = dict()
|
|
|
|
facts = {}
|
|
|
|
|
|
|
|
facts['ansible_interfaces'] = []
|
|
|
|
facts['ansible_interfaces'] = []
|
|
|
|
facts['ansible_all_ipv4_addresses'] = []
|
|
|
|
facts['ansible_all_ipv4_addresses'] = []
|
|
|
|
for nic in host.config.network.vnic:
|
|
|
|
for nic in self.host.config.network.vnic:
|
|
|
|
device = nic.device
|
|
|
|
device = nic.device
|
|
|
|
facts['ansible_interfaces'].append(device)
|
|
|
|
facts['ansible_interfaces'].append(device)
|
|
|
|
facts['ansible_all_ipv4_addresses'].append(nic.spec.ip.ipAddress)
|
|
|
|
facts['ansible_all_ipv4_addresses'].append(nic.spec.ip.ipAddress)
|
|
|
@ -104,49 +159,33 @@ def get_network_facts(host):
|
|
|
|
facts['ansible_' + device] = _tmp
|
|
|
|
facts['ansible_' + device] = _tmp
|
|
|
|
return facts
|
|
|
|
return facts
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_system_facts(self):
|
|
|
|
def get_system_facts(host):
|
|
|
|
|
|
|
|
sn = 'NA'
|
|
|
|
sn = 'NA'
|
|
|
|
for info in host.hardware.systemInfo.otherIdentifyingInfo:
|
|
|
|
for info in self.host.hardware.systemInfo.otherIdentifyingInfo:
|
|
|
|
if info.identifierType.key == 'ServiceTag':
|
|
|
|
if info.identifierType.key == 'ServiceTag':
|
|
|
|
sn = info.identifierValue
|
|
|
|
sn = info.identifierValue
|
|
|
|
facts = {
|
|
|
|
facts = {
|
|
|
|
'ansible_distribution': host.config.product.name,
|
|
|
|
'ansible_distribution': self.host.config.product.name,
|
|
|
|
'ansible_distribution_version': host.config.product.version,
|
|
|
|
'ansible_distribution_version': self.host.config.product.version,
|
|
|
|
'ansible_distribution_build': host.config.product.build,
|
|
|
|
'ansible_distribution_build': self.host.config.product.build,
|
|
|
|
'ansible_os_type': host.config.product.osType,
|
|
|
|
'ansible_os_type': self.host.config.product.osType,
|
|
|
|
'ansible_system_vendor': host.hardware.systemInfo.vendor,
|
|
|
|
'ansible_system_vendor': self.host.hardware.systemInfo.vendor,
|
|
|
|
'ansible_hostname': host.summary.config.name,
|
|
|
|
'ansible_hostname': self.host.summary.config.name,
|
|
|
|
'ansible_product_name': host.hardware.systemInfo.model,
|
|
|
|
'ansible_product_name': self.host.hardware.systemInfo.model,
|
|
|
|
'ansible_product_serial': sn,
|
|
|
|
'ansible_product_serial': sn,
|
|
|
|
'ansible_bios_date': host.hardware.biosInfo.releaseDate,
|
|
|
|
'ansible_bios_date': self.host.hardware.biosInfo.releaseDate,
|
|
|
|
'ansible_bios_version': host.hardware.biosInfo.biosVersion,
|
|
|
|
'ansible_bios_version': self.host.hardware.biosInfo.biosVersion,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return facts
|
|
|
|
return facts
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def all_facts(content):
|
|
|
|
|
|
|
|
host = find_obj(content, [vim.HostSystem], None)
|
|
|
|
|
|
|
|
ansible_facts = {}
|
|
|
|
|
|
|
|
ansible_facts.update(get_cpu_facts(host))
|
|
|
|
|
|
|
|
ansible_facts.update(get_memory_facts(host))
|
|
|
|
|
|
|
|
ansible_facts.update(get_datastore_facts(host))
|
|
|
|
|
|
|
|
ansible_facts.update(get_network_facts(host))
|
|
|
|
|
|
|
|
ansible_facts.update(get_system_facts(host))
|
|
|
|
|
|
|
|
return ansible_facts
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
def main():
|
|
|
|
|
|
|
|
|
|
|
|
argument_spec = vmware_argument_spec()
|
|
|
|
argument_spec = vmware_argument_spec()
|
|
|
|
module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=False)
|
|
|
|
module = AnsibleModule(argument_spec=argument_spec,
|
|
|
|
|
|
|
|
supports_check_mode=True)
|
|
|
|
if not HAS_PYVMOMI:
|
|
|
|
|
|
|
|
module.fail_json(msg='pyvmomi is required for this module')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
content = connect_to_api(module)
|
|
|
|
vm_host_manager = VMwareHostFactManager(module)
|
|
|
|
data = all_facts(content)
|
|
|
|
vm_host_manager.all_facts()
|
|
|
|
module.exit_json(changed=False, ansible_facts=data)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
if __name__ == '__main__':
|
|
|
|