Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2015, Joseph Callen <jcallen () csc.com>
5 # Copyright: (c) 2018, Ansible Project
6 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
7 # SPDX-License-Identifier: GPL-3.0-or-later
8
9 from __future__ import absolute_import, division, print_function
10 __metaclass__ = type
11
12
13 DOCUMENTATION = r'''
14 ---
15 module: vmware_target_canonical_info
16 short_description: Return canonical (NAA) from an ESXi host system
17 description:
18 - This module can be used to gather information about canonical (NAA) from an ESXi host based on SCSI target ID.
19 author:
20 - Joseph Callen (@jcpowermac)
21 - Abhijeet Kasurde (@Akasurde)
22 options:
23 target_id:
24 description:
25 - The target id based on order of scsi device.
26 - version 2.6 onwards, this parameter is optional.
27 required: false
28 type: int
29 cluster_name:
30 description:
31 - Name of the cluster.
32 - Info about all SCSI devices for all host system in the given cluster is returned.
33 - This parameter is required, if O(esxi_hostname) is not provided.
34 type: str
35 esxi_hostname:
36 description:
37 - Name of the ESXi host system.
38 - Info about all SCSI devices for the given ESXi host system is returned.
39 - This parameter is required, if O(cluster_name) is not provided.
40 type: str
41 extends_documentation_fragment:
42 - vmware.vmware.base_options
43
44 '''
45
46 EXAMPLES = r'''
47 - name: Get Canonical name of particular target on particular ESXi host system
48 community.vmware.vmware_target_canonical_info:
49 hostname: '{{ vcenter_hostname }}'
50 username: '{{ vcenter_username }}'
51 password: '{{ vcenter_password }}'
52 target_id: 7
53 esxi_hostname: esxi_hostname
54 delegate_to: localhost
55
56 - name: Get Canonical name of all target on particular ESXi host system
57 community.vmware.vmware_target_canonical_info:
58 hostname: '{{ vcenter_hostname }}'
59 username: '{{ vcenter_username }}'
60 password: '{{ vcenter_password }}'
61 esxi_hostname: '{{ esxi_hostname }}'
62 delegate_to: localhost
63
64 - name: Get Canonical name of all ESXi hostname on particular Cluster
65 community.vmware.vmware_target_canonical_info:
66 hostname: '{{ vcenter_hostname }}'
67 username: '{{ vcenter_username }}'
68 password: '{{ vcenter_password }}'
69 cluster_name: '{{ cluster_name }}'
70 delegate_to: localhost
71 '''
72
73 RETURN = r'''
74 canonical:
75 description: metadata about SCSI Target device
76 returned: if host system and target id is given
77 type: str
78 sample: "mpx.vmhba0:C0:T0:L0"
79
80 scsi_tgt_info:
81 description: metadata about all SCSI Target devices
82 returned: if host system or cluster is given
83 type: dict
84 sample: {
85 "DC0_C0_H0": {
86 "scsilun_canonical": {
87 "key-vim.host.ScsiDisk-0000000000766d686261303a303a30": "mpx.vmhba0:C0:T0:L0",
88 "key-vim.host.ScsiLun-0005000000766d686261313a303a30": "mpx.vmhba1:C0:T0:L0"
89 },
90 "target_lun_uuid": {
91 "0": "key-vim.host.ScsiDisk-0000000000766d686261303a303a30"
92 }
93 },
94 "DC0_C0_H1": {
95 "scsilun_canonical": {
96 "key-vim.host.ScsiDisk-0000000000766d686261303a303a30": "mpx.vmhba0:C0:T0:L0",
97 "key-vim.host.ScsiLun-0005000000766d686261313a303a30": "mpx.vmhba1:C0:T0:L0"
98 },
99 "target_lun_uuid": {
100 "0": "key-vim.host.ScsiDisk-0000000000766d686261303a303a30"
101 }
102 },
103 }
104 '''
105
106 from ansible.module_utils.basic import AnsibleModule
107 from ansible_collections.community.vmware.plugins.module_utils.vmware import PyVmomi
108 from ansible_collections.vmware.vmware.plugins.module_utils.argument_spec import base_argument_spec
109
110
111 class ScsiTargetInfoManager(PyVmomi):
112 def __init__(self, module):
113 super(ScsiTargetInfoManager, self).__init__(module)
114 cluster_name = self.module.params.get('cluster_name')
115 self.esxi_hostname = self.module.params.get('esxi_hostname')
116 self.hosts = self.get_all_host_objs(cluster_name=cluster_name, esxi_host_name=self.esxi_hostname)
117
118 def gather_scsi_device_info(self):
119 """
120 Function to gather information about SCSI target devices
121
122 """
123 scsi_tgt_info = {}
124 target_lun_uuid = {}
125 scsilun_canonical = {}
126 target_id = self.module.params['target_id']
127
128 for host in self.hosts:
129 # Associate the scsiLun key with the canonicalName (NAA)
130 for scsilun in host.config.storageDevice.scsiLun:
131 scsilun_canonical[scsilun.key] = scsilun.canonicalName
132
133 # Associate target number with LUN uuid
134 for target in host.config.storageDevice.scsiTopology.adapter[0].target:
135 for lun in target.lun:
136 target_lun_uuid[str(target.target)] = lun.scsiLun
137
138 scsi_tgt_info[host.name] = dict(
139 scsilun_canonical=scsilun_canonical,
140 target_lun_uuid=target_lun_uuid)
141
142 if target_id is not None and self.esxi_hostname is not None:
143 canonical = ''
144 temp_lun_data = scsi_tgt_info[self.esxi_hostname]['target_lun_uuid']
145 if self.esxi_hostname in scsi_tgt_info and \
146 str(target_id) in temp_lun_data:
147 temp_scsi_data = scsi_tgt_info[self.esxi_hostname]['scsilun_canonical']
148 temp_target = temp_lun_data[target_id]
149 canonical = temp_scsi_data[temp_target]
150 self.module.exit_json(changed=False, canonical=canonical)
151
152 self.module.exit_json(changed=False, scsi_tgt_info=scsi_tgt_info)
153
154
155 def main():
156 argument_spec = base_argument_spec()
157 argument_spec.update(
158 dict(
159 target_id=dict(required=False, type='int'),
160 cluster_name=dict(type='str', required=False),
161 esxi_hostname=dict(type='str', required=False),
162 )
163 )
164
165 module = AnsibleModule(
166 argument_spec=argument_spec,
167 required_one_of=[
168 ['cluster_name', 'esxi_hostname'],
169 ],
170 supports_check_mode=True,
171 )
172
173 scsi_tgt_manager = ScsiTargetInfoManager(module)
174 scsi_tgt_manager.gather_scsi_device_info()
175
176
177 if __name__ == '__main__':
178 main()