Debian Codegraph @

ansible_collections/community/vmware/plugins/modules/vmware_host_dns_info.py

raw
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_host_dns_info
15 short_description: Gathers info about an ESXi host's DNS configuration information
16 description:
17 - This module can be used to gather information about an ESXi host's DNS configuration information when ESXi hostname or Cluster name is given.
18 author:
19 - Abhijeet Kasurde (@Akasurde)
20 options:
21 cluster_name:
22 description:
23 - Name of the cluster from which the ESXi host belong to.
24 - If O(esxi_hostname) is not given, this parameter is required.
25 type: str
26 esxi_hostname:
27 description:
28 - ESXi hostname to gather information from.
29 - If O(cluster_name) is not given, this parameter is required.
30 type: str
31 extends_documentation_fragment:
32 - vmware.vmware.base_options
33
34 '''
35
36 EXAMPLES = r'''
37 - name: Gather DNS info about all ESXi Hosts in given Cluster
38 community.vmware.vmware_host_dns_info:
39 hostname: '{{ vcenter_hostname }}'
40 username: '{{ vcenter_username }}'
41 password: '{{ vcenter_password }}'
42 cluster_name: cluster_name
43 delegate_to: localhost
44
45 - name: Gather DNS info about ESXi Host
46 community.vmware.vmware_host_dns_info:
47 hostname: '{{ vcenter_hostname }}'
48 username: '{{ vcenter_username }}'
49 password: '{{ vcenter_password }}'
50 esxi_hostname: '{{ esxi_hostname }}'
51 delegate_to: localhost
52 '''
53
54 RETURN = r'''
55 hosts_dns_info:
56 description: metadata about DNS config from given cluster / host system
57 returned: always
58 type: dict
59 sample: {
60 "DC0_C0_H0": {
61 "dhcp": true,
62 "domain_name": "localdomain",
63 "host_name": "localhost",
64 "ip_address": [
65 "8.8.8.8"
66 ],
67 "search_domain": [
68 "localdomain"
69 ],
70 "virtual_nic_device": "vmk0"
71 }
72 }
73 '''
74
75 from ansible.module_utils.basic import AnsibleModule
76 from ansible_collections.community.vmware.plugins.module_utils.vmware import PyVmomi
77 from ansible_collections.vmware.vmware.plugins.module_utils.argument_spec import base_argument_spec
78
79
80 class VmwareDnsInfoManager(PyVmomi):
81 def __init__(self, module):
82 super(VmwareDnsInfoManager, self).__init__(module)
83 cluster_name = self.params.get('cluster_name', None)
84 esxi_host_name = self.params.get('esxi_hostname', None)
85 self.hosts = self.get_all_host_objs(cluster_name=cluster_name, esxi_host_name=esxi_host_name)
86
87 def gather_dns_info(self):
88 hosts_info = {}
89 for host in self.hosts:
90 host_info = {}
91 dns_config = host.config.network.dnsConfig
92 host_info['dhcp'] = dns_config.dhcp
93 host_info['virtual_nic_device'] = dns_config.virtualNicDevice
94 host_info['host_name'] = dns_config.hostName
95 host_info['domain_name'] = dns_config.domainName
96 host_info['ip_address'] = list(dns_config.address)
97 host_info['search_domain'] = list(dns_config.searchDomain)
98 hosts_info[host.name] = host_info
99 return hosts_info
100
101
102 def main():
103 argument_spec = base_argument_spec()
104 argument_spec.update(
105 cluster_name=dict(type='str', required=False),
106 esxi_hostname=dict(type='str', required=False),
107 )
108
109 module = AnsibleModule(
110 argument_spec=argument_spec,
111 required_one_of=[
112 ['cluster_name', 'esxi_hostname'],
113 ],
114 supports_check_mode=True
115 )
116
117 vmware_dns_config = VmwareDnsInfoManager(module)
118 module.exit_json(changed=False, hosts_dns_info=vmware_dns_config.gather_dns_info())
119
120
121 if __name__ == "__main__":
122 main()