Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2018, Ansible Project
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
10 __metaclass__ = type
11
12
13 DOCUMENTATION = r'''
14 ---
15 module: vmware_host_scanhba
16 short_description: Rescan host HBA's and optionally refresh the storage system
17 description:
18 - This module can force a rescan of the hosts HBA subsystem which is needed when wanting to mount a new datastore.
19 - You could use this before using M(community.vmware.vmware_host_datastore) to mount a new datastore to ensure your device/volume is ready.
20 - You can also optionally force a Refresh of the Storage System in vCenter/ESXi Web Client.
21 - You can supply an esxi_hostname or a cluster_name
22 author:
23 - Michael Eaton (@michaeldeaton)
24 options:
25 esxi_hostname:
26 description:
27 - ESXi hostname to Rescan the storage subsystem on.
28 required: false
29 type: str
30 cluster_name:
31 description:
32 - Cluster name to Rescan the storage subsystem on (this will run the rescan task on each host in the cluster).
33 required: false
34 type: str
35 rescan_hba:
36 description:
37 - Rescan all host bus adapters for new storage devices. Rescanning all adapters can be slow.
38 required: false
39 default: true
40 type: bool
41 refresh_storage:
42 description:
43 - Refresh the storage system in vCenter/ESXi Web Client for each host found
44 required: false
45 default: false
46 type: bool
47 rescan_vmfs:
48 description:
49 - Rescan all known storage devices for new VMFS volumes.
50 required: false
51 default: false
52 type: bool
53 extends_documentation_fragment:
54 - vmware.vmware.base_options
55
56 '''
57
58 EXAMPLES = r'''
59 - name: Rescan HBA's for a given ESXi host and refresh storage system objects
60 community.vmware.vmware_host_scanhba:
61 hostname: '{{ vcenter_hostname }}'
62 username: '{{ vcenter_username }}'
63 password: '{{ vcenter_password }}'
64 esxi_hostname: '{{ inventory_hostname }}'
65 refresh_storage: true
66 delegate_to: localhost
67
68 - name: Rescan HBA's for a given cluster - all found hosts will be scanned
69 community.vmware.vmware_host_scanhba:
70 hostname: '{{ vcenter_hostname }}'
71 username: '{{ vcenter_username }}'
72 password: '{{ vcenter_password }}'
73 esxi_hostname: '{{ inventory_hostname }}'
74 refresh_storage: true
75 delegate_to: localhost
76
77 - name: Rescan for new VMFS Volumes in a given cluster, but do not scan for new Devices - all found hosts will be scanned
78 community.vmware.vmware_host_scanhba:
79 hostname: '{{ vcenter_hostname }}'
80 username: '{{ vcenter_username }}'
81 password: '{{ vcenter_password }}'
82 esxi_hostname: '{{ inventory_hostname }}'
83 rescan_vmfs: true
84 rescan_hba: false
85 delegate_to: localhost
86
87 - name: Rescan HBA's for a given ESXi host and don't refresh storage system objects
88 community.vmware.vmware_host_scanhba:
89 hostname: '{{ vcenter_hostname }}'
90 username: '{{ vcenter_username }}'
91 password: '{{ vcenter_password }}'
92 esxi_hostname: '{{ inventory_hostname }}'
93 delegate_to: localhost
94 '''
95
96 RETURN = r'''
97 result:
98 description: return confirmation of requested host and updated / refreshed storage system
99 returned: always
100 type: dict
101 sample: {
102 "esxi01.example.com": {
103 "rescaned_hba": "true",
104 "refreshed_storage": "true",
105 "rescaned_vmfs": "true"
106 }
107 }
108 '''
109
110 from ansible.module_utils.basic import AnsibleModule
111 from ansible_collections.community.vmware.plugins.module_utils.vmware import PyVmomi
112 from ansible_collections.vmware.vmware.plugins.module_utils.argument_spec import base_argument_spec
113
114
115 class VmwareHbaScan(PyVmomi):
116 def __init__(self, module):
117 super(VmwareHbaScan, self).__init__(module)
118
119 def scan(self):
120 esxi_host_name = self.params.get('esxi_hostname', None)
121 cluster_name = self.params.get('cluster_name', None)
122 rescan_hba = self.params.get('rescan_hba', bool)
123 refresh_storage = self.params.get('refresh_storage', bool)
124 rescan_vmfs = self.params.get('rescan_vmfs', bool)
125 hosts = self.get_all_host_objs(cluster_name=cluster_name, esxi_host_name=esxi_host_name)
126 results = dict(changed=True, result=dict())
127
128 if not hosts:
129 self.module.fail_json(msg="Failed to find any hosts.")
130
131 for host in hosts:
132 results['result'][host.name] = dict()
133 if rescan_hba is True:
134 host.configManager.storageSystem.RescanAllHba()
135
136 if refresh_storage is True:
137 host.configManager.storageSystem.RefreshStorageSystem()
138
139 if rescan_vmfs is True:
140 host.configManager.storageSystem.RescanVmfs()
141
142 results['result'][host.name]['rescaned_hba'] = rescan_hba
143 results['result'][host.name]['refreshed_storage'] = refresh_storage
144 results['result'][host.name]['rescaned_vmfs'] = rescan_vmfs
145
146 self.module.exit_json(**results)
147
148
149 def main():
150 argument_spec = base_argument_spec()
151 argument_spec.update(
152 esxi_hostname=dict(type='str', required=False),
153 cluster_name=dict(type='str', required=False),
154 rescan_hba=dict(type='bool', default=True, required=False),
155 refresh_storage=dict(type='bool', default=False, required=False),
156 rescan_vmfs=dict(type='bool', default=False, required=False)
157 )
158 module = AnsibleModule(
159 argument_spec=argument_spec,
160 required_one_of=[
161 ['cluster_name', 'esxi_hostname'],
162 ],
163 supports_check_mode=False
164 )
165
166 hbascan = VmwareHbaScan(module)
167 hbascan.scan()
168
169
170 if __name__ == '__main__':
171 main()