| 1 |
#!/usr/bin/python |
| 2 |
# Copyright (c) 2021-2022 Hewlett Packard Enterprise, Inc. All rights reserved. |
| 3 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 4 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 5 |
from __future__ import annotations |
| 6 |
|
| 7 |
DOCUMENTATION = r""" |
| 8 |
module: ilo_redfish_command |
| 9 |
short_description: Manages Out-Of-Band controllers using Redfish APIs |
| 10 |
version_added: 6.6.0 |
| 11 |
description: |
| 12 |
- Builds Redfish URIs locally and sends them to remote OOB controllers to perform an action. |
| 13 |
attributes: |
| 14 |
check_mode: |
| 15 |
support: none |
| 16 |
diff_mode: |
| 17 |
support: none |
| 18 |
extends_documentation_fragment: |
| 19 |
- community.general._attributes |
| 20 |
- community.general._redfish |
| 21 |
options: |
| 22 |
category: |
| 23 |
required: true |
| 24 |
description: |
| 25 |
- Category to execute on OOB controller. |
| 26 |
type: str |
| 27 |
choices: ['Systems'] |
| 28 |
command: |
| 29 |
required: true |
| 30 |
description: |
| 31 |
- List of commands to execute on OOB controller. |
| 32 |
type: list |
| 33 |
elements: str |
| 34 |
baseuri: |
| 35 |
required: true |
| 36 |
description: |
| 37 |
- Base URI of OOB controller. |
| 38 |
type: str |
| 39 |
username: |
| 40 |
description: |
| 41 |
- Username for authenticating to iLO. |
| 42 |
type: str |
| 43 |
password: |
| 44 |
description: |
| 45 |
- Password for authenticating to iLO. |
| 46 |
type: str |
| 47 |
auth_token: |
| 48 |
description: |
| 49 |
- Security token for authenticating to iLO. |
| 50 |
type: str |
| 51 |
timeout: |
| 52 |
description: |
| 53 |
- Timeout in seconds for HTTP requests to iLO. |
| 54 |
default: 60 |
| 55 |
type: int |
| 56 |
validate_certs: |
| 57 |
version_added: 10.6.0 |
| 58 |
ca_path: |
| 59 |
version_added: 10.6.0 |
| 60 |
ciphers: |
| 61 |
version_added: 10.6.0 |
| 62 |
author: |
| 63 |
- Varni H P (@varini-hp) |
| 64 |
""" |
| 65 |
|
| 66 |
EXAMPLES = r""" |
| 67 |
- name: Wait for iLO Reboot Completion |
| 68 |
community.general.ilo_redfish_command: |
| 69 |
category: Systems |
| 70 |
command: WaitforiLORebootCompletion |
| 71 |
baseuri: "{{ baseuri }}" |
| 72 |
username: "{{ username }}" |
| 73 |
password: "{{ password }}" |
| 74 |
""" |
| 75 |
|
| 76 |
RETURN = r""" |
| 77 |
ilo_redfish_command: |
| 78 |
description: Returns the status of the operation performed on the iLO. |
| 79 |
type: dict |
| 80 |
contains: |
| 81 |
WaitforiLORebootCompletion: |
| 82 |
description: Returns the output msg and whether the function executed successfully. |
| 83 |
type: dict |
| 84 |
contains: |
| 85 |
ret: |
| 86 |
description: Return V(true)/V(false) based on whether the operation was performed successfully. |
| 87 |
type: bool |
| 88 |
msg: |
| 89 |
description: Status of the operation performed on the iLO. |
| 90 |
type: str |
| 91 |
returned: always |
| 92 |
""" |
| 93 |
|
| 94 |
# More will be added as module features are expanded |
| 95 |
CATEGORY_COMMANDS_ALL = {"Systems": ["WaitforiLORebootCompletion"]} |
| 96 |
|
| 97 |
from ansible.module_utils.basic import AnsibleModule |
| 98 |
|
| 99 |
from ansible_collections.community.general.plugins.module_utils._ilo_redfish_utils import iLORedfishUtils |
| 100 |
from ansible_collections.community.general.plugins.module_utils._redfish_utils import REDFISH_COMMON_ARGUMENT_SPEC |
| 101 |
|
| 102 |
|
| 103 |
def main(): |
| 104 |
result = {} |
| 105 |
argument_spec = dict( |
| 106 |
category=dict(required=True, choices=list(CATEGORY_COMMANDS_ALL.keys())), |
| 107 |
command=dict(required=True, type="list", elements="str"), |
| 108 |
baseuri=dict(required=True), |
| 109 |
timeout=dict(type="int", default=60), |
| 110 |
username=dict(), |
| 111 |
password=dict(no_log=True), |
| 112 |
auth_token=dict(no_log=True), |
| 113 |
) |
| 114 |
argument_spec.update(REDFISH_COMMON_ARGUMENT_SPEC) |
| 115 |
module = AnsibleModule( |
| 116 |
argument_spec, |
| 117 |
required_together=[ |
| 118 |
("username", "password"), |
| 119 |
], |
| 120 |
required_one_of=[ |
| 121 |
("username", "auth_token"), |
| 122 |
], |
| 123 |
mutually_exclusive=[ |
| 124 |
("username", "auth_token"), |
| 125 |
], |
| 126 |
supports_check_mode=False, |
| 127 |
) |
| 128 |
|
| 129 |
category = module.params["category"] |
| 130 |
command_list = module.params["command"] |
| 131 |
|
| 132 |
# admin credentials used for authentication |
| 133 |
creds = {"user": module.params["username"], "pswd": module.params["password"], "token": module.params["auth_token"]} |
| 134 |
|
| 135 |
timeout = module.params["timeout"] |
| 136 |
|
| 137 |
# Build root URI |
| 138 |
root_uri = f"https://{module.params['baseuri']}" |
| 139 |
rf_utils = iLORedfishUtils(creds, root_uri, timeout, module) |
| 140 |
|
| 141 |
# Check that Category is valid |
| 142 |
if category not in CATEGORY_COMMANDS_ALL: |
| 143 |
module.fail_json(msg=f"Invalid Category '{category}'. Valid Categories = {list(CATEGORY_COMMANDS_ALL.keys())}") |
| 144 |
|
| 145 |
# Check that all commands are valid |
| 146 |
for cmd in command_list: |
| 147 |
# Fail if even one command given is invalid |
| 148 |
if cmd not in CATEGORY_COMMANDS_ALL[category]: |
| 149 |
module.fail_json(msg=f"Invalid Command '{cmd}'. Valid Commands = {CATEGORY_COMMANDS_ALL[category]}") |
| 150 |
|
| 151 |
if category == "Systems": |
| 152 |
# execute only if we find a System resource |
| 153 |
|
| 154 |
result = rf_utils._find_systems_resource() |
| 155 |
if result["ret"] is False: |
| 156 |
module.fail_json(msg=result["msg"]) |
| 157 |
|
| 158 |
for command in command_list: |
| 159 |
if command == "WaitforiLORebootCompletion": |
| 160 |
result[command] = rf_utils.wait_for_ilo_reboot_completion() |
| 161 |
|
| 162 |
# Return data back or fail with proper message |
| 163 |
if not result[command]["ret"]: |
| 164 |
module.fail_json(msg=result) |
| 165 |
|
| 166 |
changed = result[command].get("changed", False) |
| 167 |
module.exit_json(ilo_redfish_command=result, changed=changed) |
| 168 |
|
| 169 |
|
| 170 |
if __name__ == "__main__": |
| 171 |
main() |