Debian Codegraph @

ansible_collections/community/general/plugins/modules/hpilo_boot.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_boot
11 author: Dag Wieers (@dagwieers)
12 short_description: Boot system using specific media through HP iLO interface
13 description:
14 - 'This module boots a system through its HP iLO interface. The boot media can be one of: V(cdrom), V(floppy), V(hdd), V(network),
15 or V(usb).'
16 - This module requires the hpilo python module.
17 extends_documentation_fragment:
18 - community.general._attributes
19 attributes:
20 check_mode:
21 support: none
22 diff_mode:
23 support: none
24 options:
25 host:
26 description:
27 - The HP iLO hostname/address that is linked to the physical system.
28 type: str
29 required: true
30 login:
31 description:
32 - The login name to authenticate to the HP iLO interface.
33 default: Administrator
34 type: str
35 password:
36 description:
37 - The password to authenticate to the HP iLO interface.
38 default: admin
39 type: str
40 media:
41 description:
42 - The boot media to boot the system from.
43 choices: ["cdrom", "floppy", "rbsu", "hdd", "network", "normal", "usb"]
44 type: str
45 image:
46 description:
47 - The URL of a cdrom, floppy or usb boot media image in the form V(protocol://username:password@hostname:port/filename).
48 - V(protocol) is either V(http) or V(https).
49 - V(username:password) is optional.
50 - V(port) is optional.
51 type: str
52 state:
53 description:
54 - The state of the boot media.
55 - 'V(no_boot): Do not boot from the device.'
56 - 'V(boot_once): Boot from the device once and then notthereafter.'
57 - 'V(boot_always): Boot from the device each time the server is rebooted.'
58 - 'V(connect): Connect the virtual media device and set to boot_always.'
59 - 'V(disconnect): Disconnects the virtual media device and set to no_boot.'
60 - 'V(poweroff): Power off the server.'
61 default: boot_once
62 type: str
63 choices: ["boot_always", "boot_once", "connect", "disconnect", "no_boot", "poweroff"]
64 force:
65 description:
66 - Whether to force a reboot (even when the system is already booted).
67 - As a safeguard, without force, M(community.general.hpilo_boot) refuses to reboot a server that is already running.
68 default: false
69 type: bool
70 ssl_version:
71 description:
72 - Change the ssl_version used.
73 default: TLSv1
74 type: str
75 choices: ["SSLv3", "SSLv23", "TLSv1", "TLSv1_1", "TLSv1_2"]
76 idempotent_boot_once:
77 description:
78 - This option makes O(state=boot_once) succeed instead of failing when the server is already powered on.
79 type: bool
80 default: false
81 version_added: 10.6.0
82 requirements:
83 - python-hpilo
84 notes:
85 - To use a USB key image you need to specify floppy as boot media.
86 - This module ought to be run from a system that can access the HP iLO interface directly, either by using C(local_action)
87 or using C(delegate_to).
88 """
89
90 EXAMPLES = r"""
91 - name: Task to boot a system using an ISO from an HP iLO interface only if the system is an HP server
92 community.general.hpilo_boot:
93 host: YOUR_ILO_ADDRESS
94 login: YOUR_ILO_LOGIN
95 password: YOUR_ILO_PASSWORD
96 media: cdrom
97 image: http://some-web-server/iso/boot.iso
98 when: cmdb_hwmodel.startswith('HP ')
99 delegate_to: localhost
100
101 - name: Power off a server
102 community.general.hpilo_boot:
103 host: YOUR_ILO_HOST
104 login: YOUR_ILO_LOGIN
105 password: YOUR_ILO_PASSWORD
106 state: poweroff
107 delegate_to: localhost
108 """
109
110 RETURN = r"""
111 # Default return values
112 """
113
114 import time
115 import traceback
116 import warnings
117
118 HPILO_IMP_ERR = None
119 try:
120 import hpilo
121
122 HAS_HPILO = True
123 except ImportError:
124 HPILO_IMP_ERR = traceback.format_exc()
125 HAS_HPILO = False
126
127 from ansible.module_utils.basic import AnsibleModule, missing_required_lib
128
129 # Suppress warnings from hpilo
130 warnings.simplefilter("ignore")
131
132
133 def main():
134 module = AnsibleModule(
135 argument_spec=dict(
136 host=dict(type="str", required=True),
137 login=dict(type="str", default="Administrator"),
138 password=dict(type="str", default="admin", no_log=True),
139 media=dict(type="str", choices=["cdrom", "floppy", "rbsu", "hdd", "network", "normal", "usb"]),
140 image=dict(type="str"),
141 state=dict(
142 type="str",
143 default="boot_once",
144 choices=["boot_always", "boot_once", "connect", "disconnect", "no_boot", "poweroff"],
145 ),
146 force=dict(type="bool", default=False),
147 idempotent_boot_once=dict(type="bool", default=False),
148 ssl_version=dict(type="str", default="TLSv1", choices=["SSLv3", "SSLv23", "TLSv1", "TLSv1_1", "TLSv1_2"]),
149 )
150 )
151
152 if not HAS_HPILO:
153 module.fail_json(msg=missing_required_lib("python-hpilo"), exception=HPILO_IMP_ERR)
154
155 host = module.params["host"]
156 login = module.params["login"]
157 password = module.params["password"]
158 media = module.params["media"]
159 image = module.params["image"]
160 state = module.params["state"]
161 force = module.params["force"]
162 idempotent_boot_once = module.params["idempotent_boot_once"]
163 ssl_version = getattr(hpilo.ssl, f"PROTOCOL_{module.params.get('ssl_version').upper().replace('V', 'v')}")
164
165 ilo = hpilo.Ilo(host, login=login, password=password, ssl_version=ssl_version)
166 changed = False
167 status = {}
168 power_status = "UNKNOWN"
169
170 if media and state in ("boot_always", "boot_once", "connect", "disconnect", "no_boot"):
171 # Workaround for: Error communicating with iLO: Problem manipulating EV
172 try:
173 ilo.set_one_time_boot(media)
174 except hpilo.IloError:
175 time.sleep(60)
176 ilo.set_one_time_boot(media)
177
178 # TODO: Verify if image URL exists/works
179 if image:
180 ilo.insert_virtual_media(media, image)
181 changed = True
182
183 if media == "cdrom":
184 ilo.set_vm_status("cdrom", state, True)
185 status = ilo.get_vm_status()
186 changed = True
187 elif media in ("floppy", "usb"):
188 ilo.set_vf_status(state, True)
189 status = ilo.get_vf_status()
190 changed = True
191
192 # Only perform a boot when state is boot_once or boot_always, or in case we want to force a reboot
193 if state in ("boot_once", "boot_always") or force:
194 power_status = ilo.get_host_power_status()
195
196 if power_status == "ON":
197 if not force and not idempotent_boot_once:
198 # module.deprecate(
199 # 'The failure of the module when the server is already powered on is being deprecated.'
200 # ' Please set the parameter "idempotent_boot_once=true" to start using the new behavior.',
201 # version='11.0.0',
202 # collection_name='community.general'
203 # )
204 module.fail_json(msg=f"HP iLO ({host}) reports that the server is already powered on !")
205 elif not force and idempotent_boot_once:
206 pass
207 elif force:
208 ilo.warm_boot_server()
209 # ilo.cold_boot_server()
210 changed = True
211 else:
212 ilo.press_pwr_btn()
213 # ilo.reset_server()
214 # ilo.set_host_power(host_power=True)
215 changed = True
216
217 elif state in ("poweroff"):
218 power_status = ilo.get_host_power_status()
219
220 if power_status != "OFF":
221 ilo.hold_pwr_btn()
222 # ilo.set_host_power(host_power=False)
223 changed = True
224
225 module.exit_json(changed=changed, power=power_status, **status)
226
227
228 if __name__ == "__main__":
229 main()