| 1 |
#!/usr/bin/python |
| 2 |
# |
| 3 |
# Copyright Ansible Project |
| 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: ipmi_boot |
| 11 |
short_description: Management of order of boot devices |
| 12 |
description: |
| 13 |
- Use this module to manage order of boot devices. |
| 14 |
extends_documentation_fragment: |
| 15 |
- community.general._attributes |
| 16 |
attributes: |
| 17 |
check_mode: |
| 18 |
support: full |
| 19 |
diff_mode: |
| 20 |
support: none |
| 21 |
options: |
| 22 |
name: |
| 23 |
description: |
| 24 |
- Hostname or IP address of the BMC. |
| 25 |
required: true |
| 26 |
type: str |
| 27 |
port: |
| 28 |
description: |
| 29 |
- Remote RMCP port. |
| 30 |
default: 623 |
| 31 |
type: int |
| 32 |
user: |
| 33 |
description: |
| 34 |
- Username to use to connect to the BMC. |
| 35 |
required: true |
| 36 |
type: str |
| 37 |
password: |
| 38 |
description: |
| 39 |
- Password to connect to the BMC. |
| 40 |
required: true |
| 41 |
type: str |
| 42 |
key: |
| 43 |
description: |
| 44 |
- Encryption key to connect to the BMC in hex format. |
| 45 |
type: str |
| 46 |
version_added: 4.1.0 |
| 47 |
bootdev: |
| 48 |
description: |
| 49 |
- Set boot device to use on next reboot. |
| 50 |
- 'The choices for the device are:' |
| 51 |
- V(network) -- Request network boot. |
| 52 |
- V(floppy) -- Boot from floppy. |
| 53 |
- V(hd) -- Boot from hard drive. |
| 54 |
- V(safe) -- Boot from hard drive, requesting 'safe mode'. |
| 55 |
- V(optical) -- boot from CD/DVD/BD drive. |
| 56 |
- V(setup) -- Boot into setup utility. |
| 57 |
- V(default) -- remove any IPMI directed boot device request. |
| 58 |
required: true |
| 59 |
choices: |
| 60 |
- network |
| 61 |
- floppy |
| 62 |
- hd |
| 63 |
- safe |
| 64 |
- optical |
| 65 |
- setup |
| 66 |
- default |
| 67 |
type: str |
| 68 |
state: |
| 69 |
description: |
| 70 |
- Whether to ensure that boot devices is desired. |
| 71 |
- 'The choices for the state are: - present -- Request system turn on - absent -- Request system turn on.' |
| 72 |
default: present |
| 73 |
choices: [present, absent] |
| 74 |
type: str |
| 75 |
persistent: |
| 76 |
description: |
| 77 |
- If set, ask that system firmware uses this device beyond next boot. Be aware many systems do not honor this. |
| 78 |
type: bool |
| 79 |
default: false |
| 80 |
uefiboot: |
| 81 |
description: |
| 82 |
- If set, request UEFI boot explicitly. Strictly speaking, the spec suggests that if not set, the system should BIOS |
| 83 |
boot and offers no "do not care" option. In practice, this flag not being set does not preclude UEFI boot on any system |
| 84 |
I have encountered. |
| 85 |
type: bool |
| 86 |
default: false |
| 87 |
requirements: |
| 88 |
- pyghmi |
| 89 |
author: "Bulat Gaifullin (@bgaifullin) <[email protected]>" |
| 90 |
""" |
| 91 |
|
| 92 |
RETURN = r""" |
| 93 |
bootdev: |
| 94 |
description: The boot device name which is used beyond next boot. |
| 95 |
returned: success |
| 96 |
type: str |
| 97 |
sample: default |
| 98 |
persistent: |
| 99 |
description: If V(true), system firmware uses this device beyond next boot. |
| 100 |
returned: success |
| 101 |
type: bool |
| 102 |
sample: false |
| 103 |
uefimode: |
| 104 |
description: If V(true), system firmware uses UEFI boot explicitly beyond next boot. |
| 105 |
returned: success |
| 106 |
type: bool |
| 107 |
sample: false |
| 108 |
""" |
| 109 |
|
| 110 |
EXAMPLES = r""" |
| 111 |
- name: Ensure bootdevice is HD |
| 112 |
community.general.ipmi_boot: |
| 113 |
name: test.testdomain.com |
| 114 |
user: admin |
| 115 |
password: password |
| 116 |
bootdev: hd |
| 117 |
|
| 118 |
- name: Ensure bootdevice is not Network |
| 119 |
community.general.ipmi_boot: |
| 120 |
name: test.testdomain.com |
| 121 |
user: admin |
| 122 |
password: password |
| 123 |
key: 1234567890AABBCCDEFF000000EEEE12 |
| 124 |
bootdev: network |
| 125 |
state: absent |
| 126 |
""" |
| 127 |
|
| 128 |
import binascii |
| 129 |
import traceback |
| 130 |
|
| 131 |
PYGHMI_IMP_ERR = None |
| 132 |
try: |
| 133 |
from pyghmi.ipmi import command |
| 134 |
except ImportError: |
| 135 |
PYGHMI_IMP_ERR = traceback.format_exc() |
| 136 |
command = None |
| 137 |
|
| 138 |
from ansible.module_utils.basic import AnsibleModule, missing_required_lib |
| 139 |
|
| 140 |
|
| 141 |
def main(): |
| 142 |
module = AnsibleModule( |
| 143 |
argument_spec=dict( |
| 144 |
name=dict(required=True), |
| 145 |
port=dict(default=623, type="int"), |
| 146 |
user=dict(required=True, no_log=True), |
| 147 |
password=dict(required=True, no_log=True), |
| 148 |
key=dict(type="str", no_log=True), |
| 149 |
state=dict(default="present", choices=["present", "absent"]), |
| 150 |
bootdev=dict(required=True, choices=["network", "hd", "floppy", "safe", "optical", "setup", "default"]), |
| 151 |
persistent=dict(default=False, type="bool"), |
| 152 |
uefiboot=dict(default=False, type="bool"), |
| 153 |
), |
| 154 |
supports_check_mode=True, |
| 155 |
) |
| 156 |
|
| 157 |
if command is None: |
| 158 |
module.fail_json(msg=missing_required_lib("pyghmi"), exception=PYGHMI_IMP_ERR) |
| 159 |
|
| 160 |
name = module.params["name"] |
| 161 |
port = module.params["port"] |
| 162 |
user = module.params["user"] |
| 163 |
password = module.params["password"] |
| 164 |
state = module.params["state"] |
| 165 |
bootdev = module.params["bootdev"] |
| 166 |
persistent = module.params["persistent"] |
| 167 |
uefiboot = module.params["uefiboot"] |
| 168 |
request = dict() |
| 169 |
|
| 170 |
if state == "absent" and bootdev == "default": |
| 171 |
module.fail_json(msg="The bootdev 'default' cannot be used with state 'absent'.") |
| 172 |
|
| 173 |
try: |
| 174 |
if module.params["key"]: |
| 175 |
key = binascii.unhexlify(module.params["key"]) |
| 176 |
else: |
| 177 |
key = None |
| 178 |
except Exception: |
| 179 |
module.fail_json(msg="Unable to convert 'key' from hex string.") |
| 180 |
|
| 181 |
# --- run command --- |
| 182 |
try: |
| 183 |
ipmi_cmd = command.Command(bmc=name, userid=user, password=password, port=port, kg=key) |
| 184 |
module.debug(f'ipmi instantiated - name: "{name}"') |
| 185 |
current = ipmi_cmd.get_bootdev() |
| 186 |
# uefimode may not supported by BMC, so use desired value as default |
| 187 |
current.setdefault("uefimode", uefiboot) |
| 188 |
if state == "present" and current != dict(bootdev=bootdev, persistent=persistent, uefimode=uefiboot): |
| 189 |
request = dict(bootdev=bootdev, uefiboot=uefiboot, persist=persistent) |
| 190 |
elif state == "absent" and current["bootdev"] == bootdev: |
| 191 |
request = dict(bootdev="default") |
| 192 |
else: |
| 193 |
module.exit_json(changed=False, **current) |
| 194 |
|
| 195 |
if module.check_mode: |
| 196 |
response = dict(bootdev=request["bootdev"]) |
| 197 |
else: |
| 198 |
response = ipmi_cmd.set_bootdev(**request) |
| 199 |
|
| 200 |
if "error" in response: |
| 201 |
module.fail_json(msg=response["error"]) |
| 202 |
|
| 203 |
if "persist" in request: |
| 204 |
response["persistent"] = request["persist"] |
| 205 |
if "uefiboot" in request: |
| 206 |
response["uefimode"] = request["uefiboot"] |
| 207 |
|
| 208 |
module.exit_json(changed=True, **response) |
| 209 |
except Exception as e: |
| 210 |
module.fail_json(msg=str(e)) |
| 211 |
|
| 212 |
|
| 213 |
if __name__ == "__main__": |
| 214 |
main() |