Debian Codegraph @

ansible_collections/community/general/plugins/modules/hpilo_info.py

raw
1 #!/usr/bin/python
2
3 # Copyright 2012 Dag Wieers <[email protected]>
4 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
5 # SPDX-License-Identifier: GPL-3.0-or-later
6
7 from __future__ import annotations
8
9 DOCUMENTATION = r"""
10 module: hpilo_info
11 author: Dag Wieers (@dagwieers)
12 short_description: Gather information through an HP iLO interface
13 description:
14 - This module gathers information on a specific system using its HP iLO interface. These information includes hardware and
15 network related information useful for provisioning (for example macaddress, uuid).
16 - This module requires the C(hpilo) python module.
17 extends_documentation_fragment:
18 - community.general._attributes
19 - community.general._attributes.info_module
20 options:
21 host:
22 description:
23 - The HP iLO hostname/address that is linked to the physical system.
24 type: str
25 required: true
26 login:
27 description:
28 - The login name to authenticate to the HP iLO interface.
29 type: str
30 default: Administrator
31 password:
32 description:
33 - The password to authenticate to the HP iLO interface.
34 type: str
35 default: admin
36 ssl_version:
37 description:
38 - Change the ssl_version used.
39 default: TLSv1
40 type: str
41 choices: ["SSLv3", "SSLv23", "TLSv1", "TLSv1_1", "TLSv1_2"]
42 requirements:
43 - hpilo
44 notes:
45 - This module ought to be run from a system that can access the HP iLO interface directly, either by using C(local_action)
46 or using C(delegate_to).
47 """
48
49 EXAMPLES = r"""
50 - name: Gather facts from a HP iLO interface only if the system is an HP server
51 community.general.hpilo_info:
52 host: YOUR_ILO_ADDRESS
53 login: YOUR_ILO_LOGIN
54 password: YOUR_ILO_PASSWORD
55 when: cmdb_hwmodel.startswith('HP ')
56 delegate_to: localhost
57 register: results
58
59 - ansible.builtin.fail:
60 msg: 'CMDB serial ({{ cmdb_serialno }}) does not match hardware serial ({{ results.hw_system_serial }}) !'
61 when: cmdb_serialno != results.hw_system_serial
62 """
63
64 RETURN = r"""
65 # Typical output of HP iLO_info for a physical system
66 hw_bios_date:
67 description: BIOS date.
68 returned: always
69 type: str
70 sample: 05/05/2011
71
72 hw_bios_version:
73 description: BIOS version.
74 returned: always
75 type: str
76 sample: P68
77
78 hw_ethX:
79 description: Interface information (for each interface).
80 returned: always
81 type: dict
82 sample:
83 - macaddress: 00:11:22:33:44:55
84 macaddress_dash: 00-11-22-33-44-55
85
86 hw_eth_ilo:
87 description: Interface information (for the iLO network interface).
88 returned: always
89 type: dict
90 sample:
91 - macaddress: 00:11:22:33:44:BA
92 - macaddress_dash: 00-11-22-33-44-BA
93
94 hw_product_name:
95 description: Product name.
96 returned: always
97 type: str
98 sample: ProLiant DL360 G7
99
100 hw_product_uuid:
101 description: Product UUID.
102 returned: always
103 type: str
104 sample: ef50bac8-2845-40ff-81d9-675315501dac
105
106 hw_system_serial:
107 description: System serial number.
108 returned: always
109 type: str
110 sample: ABC12345D6
111
112 hw_uuid:
113 description: Hardware UUID.
114 returned: always
115 type: str
116 sample: 123456ABC78901D2
117
118 host_power_status:
119 description:
120 - Power status of host.
121 - It is one of V(ON), V(OFF) and V(UNKNOWN).
122 returned: always
123 type: str
124 sample: "ON"
125 version_added: 3.5.0
126 """
127
128 import re
129 import traceback
130 import warnings
131
132 HPILO_IMP_ERR = None
133 try:
134 import hpilo
135
136 HAS_HPILO = True
137 except ImportError:
138 HPILO_IMP_ERR = traceback.format_exc()
139 HAS_HPILO = False
140
141 from ansible.module_utils.basic import AnsibleModule, missing_required_lib
142
143 # Suppress warnings from hpilo
144 warnings.simplefilter("ignore")
145
146
147 def parse_flat_interface(entry, non_numeric="hw_eth_ilo"):
148 try:
149 infoname = f"hw_eth{int(entry['Port']) - 1}"
150 except Exception:
151 infoname = non_numeric
152
153 info = {"macaddress": entry["MAC"].replace("-", ":"), "macaddress_dash": entry["MAC"]}
154 return (infoname, info)
155
156
157 def main():
158 module = AnsibleModule(
159 argument_spec=dict(
160 host=dict(type="str", required=True),
161 login=dict(type="str", default="Administrator"),
162 password=dict(type="str", default="admin", no_log=True),
163 ssl_version=dict(type="str", default="TLSv1", choices=["SSLv3", "SSLv23", "TLSv1", "TLSv1_1", "TLSv1_2"]),
164 ),
165 supports_check_mode=True,
166 )
167
168 if not HAS_HPILO:
169 module.fail_json(msg=missing_required_lib("python-hpilo"), exception=HPILO_IMP_ERR)
170
171 host = module.params["host"]
172 login = module.params["login"]
173 password = module.params["password"]
174 ssl_version = getattr(hpilo.ssl, f"PROTOCOL_{module.params.get('ssl_version').upper().replace('V', 'v')}")
175
176 ilo = hpilo.Ilo(host, login=login, password=password, ssl_version=ssl_version)
177
178 info = {
179 "module_hw": True,
180 }
181
182 # TODO: Count number of CPUs, DIMMs and total memory
183 try:
184 data = ilo.get_host_data()
185 power_state = ilo.get_host_power_status()
186 except hpilo.IloCommunicationError as e:
187 module.fail_json(msg=f"{e}")
188
189 for entry in data:
190 if "type" not in entry:
191 continue
192 elif entry["type"] == 0: # BIOS Information
193 info["hw_bios_version"] = entry["Family"]
194 info["hw_bios_date"] = entry["Date"]
195 elif entry["type"] == 1: # System Information
196 info["hw_uuid"] = entry["UUID"]
197 info["hw_system_serial"] = entry["Serial Number"].rstrip()
198 info["hw_product_name"] = entry["Product Name"]
199 info["hw_product_uuid"] = entry["cUUID"]
200 elif entry["type"] == 209: # Embedded NIC MAC Assignment
201 if "fields" in entry:
202 for name, value in [(e["name"], e["value"]) for e in entry["fields"]]:
203 if name.startswith("Port"):
204 try:
205 infoname = f"hw_eth{int(value) - 1}"
206 except Exception:
207 infoname = "hw_eth_ilo"
208 elif name.startswith("MAC"):
209 info[infoname] = {"macaddress": value.replace("-", ":"), "macaddress_dash": value}
210 else:
211 (infoname, entry_info) = parse_flat_interface(entry, "hw_eth_ilo")
212 info[infoname] = entry_info
213 elif entry["type"] == 209: # HPQ NIC iSCSI MAC Info
214 for name, value in [(e["name"], e["value"]) for e in entry["fields"]]:
215 if name.startswith("Port"):
216 try:
217 infoname = f"hw_iscsi{int(value) - 1}"
218 except Exception:
219 infoname = "hw_iscsi_ilo"
220 elif name.startswith("MAC"):
221 info[infoname] = {"macaddress": value.replace("-", ":"), "macaddress_dash": value}
222 elif entry["type"] == 233: # Embedded NIC MAC Assignment (Alternate data format)
223 (infoname, entry_info) = parse_flat_interface(entry, "hw_eth_ilo")
224 info[infoname] = entry_info
225
226 # Collect health (RAM/CPU data)
227 health = ilo.get_embedded_health()
228 info["hw_health"] = health
229
230 memory_details_summary = health.get("memory", {}).get("memory_details_summary")
231 # RAM as reported by iLO 2.10 on ProLiant BL460c Gen8
232 if memory_details_summary:
233 info["hw_memory_details_summary"] = memory_details_summary
234 info["hw_memory_total"] = 0
235 for details in memory_details_summary.values():
236 cpu_total_memory_size = details.get("total_memory_size")
237 if cpu_total_memory_size:
238 ram = re.search(r"(\d+)\s+(\w+)", cpu_total_memory_size)
239 if ram:
240 if ram.group(2) == "GB":
241 info["hw_memory_total"] = info["hw_memory_total"] + int(ram.group(1))
242
243 # reformat into a text friendly format
244 info["hw_memory_total"] = f"{info['hw_memory_total']} GB"
245
246 # Report host state
247 info["host_power_status"] = power_state or "UNKNOWN"
248
249 module.exit_json(**info)
250
251
252 if __name__ == "__main__":
253 main()