2020-03-09 10:11:07 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# Copyright (c) 2018 Dell EMC Inc.
|
2022-08-05 12:28:29 +02:00
|
|
|
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
__metaclass__ = type
|
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: idrac_redfish_command
|
|
|
|
short_description: Manages Out-Of-Band controllers using iDRAC OEM Redfish APIs
|
|
|
|
description:
|
|
|
|
- Builds Redfish URIs locally and sends them to remote OOB controllers to
|
|
|
|
perform an action.
|
2022-05-24 20:07:10 +02:00
|
|
|
- For use with Dell iDRAC operations that require Redfish OEM extensions.
|
2023-02-20 17:28:13 +01:00
|
|
|
extends_documentation_fragment:
|
|
|
|
- community.general.attributes
|
|
|
|
attributes:
|
|
|
|
check_mode:
|
|
|
|
support: none
|
|
|
|
diff_mode:
|
|
|
|
support: none
|
2020-03-09 10:11:07 +01:00
|
|
|
options:
|
|
|
|
category:
|
|
|
|
required: true
|
|
|
|
description:
|
2022-05-24 20:07:10 +02:00
|
|
|
- Category to execute on iDRAC.
|
2020-03-09 10:11:07 +01:00
|
|
|
type: str
|
|
|
|
command:
|
|
|
|
required: true
|
|
|
|
description:
|
2022-05-24 20:07:10 +02:00
|
|
|
- List of commands to execute on iDRAC.
|
2020-03-09 10:11:07 +01:00
|
|
|
type: list
|
2021-02-12 06:47:23 +01:00
|
|
|
elements: str
|
2020-03-09 10:11:07 +01:00
|
|
|
baseuri:
|
|
|
|
required: true
|
|
|
|
description:
|
2022-05-24 20:07:10 +02:00
|
|
|
- Base URI of iDRAC.
|
2020-03-09 10:11:07 +01:00
|
|
|
type: str
|
|
|
|
username:
|
|
|
|
description:
|
2022-05-24 20:07:10 +02:00
|
|
|
- Username for authenticating to iDRAC.
|
2020-03-09 10:11:07 +01:00
|
|
|
type: str
|
|
|
|
password:
|
|
|
|
description:
|
2022-05-24 20:07:10 +02:00
|
|
|
- Password for authenticating to iDRAC.
|
2020-03-09 10:11:07 +01:00
|
|
|
type: str
|
2021-03-19 21:14:33 +01:00
|
|
|
auth_token:
|
|
|
|
description:
|
2022-05-24 20:07:10 +02:00
|
|
|
- Security token for authenticating to iDRAC.
|
2021-03-19 21:14:33 +01:00
|
|
|
type: str
|
|
|
|
version_added: 2.3.0
|
2020-03-09 10:11:07 +01:00
|
|
|
timeout:
|
|
|
|
description:
|
2022-05-24 20:07:10 +02:00
|
|
|
- Timeout in seconds for HTTP requests to iDRAC.
|
2020-03-09 10:11:07 +01:00
|
|
|
default: 10
|
|
|
|
type: int
|
|
|
|
resource_id:
|
|
|
|
required: false
|
|
|
|
description:
|
2022-05-24 20:07:10 +02:00
|
|
|
- ID of the System, Manager or Chassis to modify.
|
2020-03-09 10:11:07 +01:00
|
|
|
type: str
|
2020-06-13 15:01:19 +02:00
|
|
|
version_added: '0.2.0'
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
author: "Jose Delarosa (@jose-delarosa)"
|
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
|
|
|
- name: Create BIOS configuration job (schedule BIOS setting update)
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.idrac_redfish_command:
|
2020-03-09 10:11:07 +01:00
|
|
|
category: Systems
|
|
|
|
command: CreateBiosConfigJob
|
|
|
|
resource_id: System.Embedded.1
|
|
|
|
baseuri: "{{ baseuri }}"
|
|
|
|
username: "{{ username }}"
|
|
|
|
password: "{{ password }}"
|
|
|
|
'''
|
|
|
|
|
|
|
|
RETURN = '''
|
|
|
|
msg:
|
|
|
|
description: Message with action result or error description
|
|
|
|
returned: always
|
|
|
|
type: str
|
|
|
|
sample: "Action was successful"
|
2023-04-19 20:11:57 +02:00
|
|
|
return_values:
|
|
|
|
description: Dictionary containing command-specific response data from the action.
|
|
|
|
returned: on success
|
|
|
|
type: dict
|
|
|
|
version_added: 6.6.0
|
|
|
|
sample: {
|
|
|
|
"job_id": "/redfish/v1/Managers/iDRAC.Embedded.1/Jobs/JID_471269252011"
|
|
|
|
}
|
2020-03-09 10:11:07 +01:00
|
|
|
'''
|
|
|
|
|
|
|
|
import re
|
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
|
|
|
from ansible_collections.community.general.plugins.module_utils.redfish_utils import RedfishUtils
|
2021-06-26 23:59:11 +02:00
|
|
|
from ansible.module_utils.common.text.converters import to_native
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
class IdracRedfishUtils(RedfishUtils):
|
|
|
|
|
|
|
|
def create_bios_config_job(self):
|
|
|
|
result = {}
|
|
|
|
key = "Bios"
|
|
|
|
jobs = "Jobs"
|
|
|
|
|
|
|
|
# Search for 'key' entry and extract URI from it
|
|
|
|
response = self.get_request(self.root_uri + self.systems_uris[0])
|
|
|
|
if response['ret'] is False:
|
|
|
|
return response
|
|
|
|
result['ret'] = True
|
|
|
|
data = response['data']
|
|
|
|
|
|
|
|
if key not in data:
|
|
|
|
return {'ret': False, 'msg': "Key %s not found" % key}
|
|
|
|
|
|
|
|
bios_uri = data[key]["@odata.id"]
|
|
|
|
|
|
|
|
# Extract proper URI
|
|
|
|
response = self.get_request(self.root_uri + bios_uri)
|
|
|
|
if response['ret'] is False:
|
|
|
|
return response
|
|
|
|
result['ret'] = True
|
|
|
|
data = response['data']
|
|
|
|
set_bios_attr_uri = data["@Redfish.Settings"]["SettingsObject"][
|
|
|
|
"@odata.id"]
|
|
|
|
|
|
|
|
payload = {"TargetSettingsURI": set_bios_attr_uri}
|
|
|
|
response = self.post_request(
|
|
|
|
self.root_uri + self.manager_uri + "/" + jobs, payload)
|
|
|
|
if response['ret'] is False:
|
|
|
|
return response
|
|
|
|
|
|
|
|
response_output = response['resp'].__dict__
|
2023-04-19 20:11:57 +02:00
|
|
|
job_id_full = response_output["headers"]["Location"]
|
|
|
|
job_id = re.search("JID_.+", job_id_full).group()
|
|
|
|
return {'ret': True, 'msg': "Config job %s created" % job_id, 'job_id': job_id_full}
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
CATEGORY_COMMANDS_ALL = {
|
|
|
|
"Systems": ["CreateBiosConfigJob"],
|
|
|
|
"Accounts": [],
|
|
|
|
"Manager": []
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
result = {}
|
2023-04-19 20:11:57 +02:00
|
|
|
return_values = {}
|
2020-03-09 10:11:07 +01:00
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec=dict(
|
|
|
|
category=dict(required=True),
|
2021-02-12 06:47:23 +01:00
|
|
|
command=dict(required=True, type='list', elements='str'),
|
2020-03-09 10:11:07 +01:00
|
|
|
baseuri=dict(required=True),
|
2021-03-19 21:14:33 +01:00
|
|
|
username=dict(),
|
|
|
|
password=dict(no_log=True),
|
|
|
|
auth_token=dict(no_log=True),
|
2020-03-09 10:11:07 +01:00
|
|
|
timeout=dict(type='int', default=10),
|
|
|
|
resource_id=dict()
|
|
|
|
),
|
2021-03-19 21:14:33 +01:00
|
|
|
required_together=[
|
|
|
|
('username', 'password'),
|
|
|
|
],
|
|
|
|
required_one_of=[
|
|
|
|
('username', 'auth_token'),
|
|
|
|
],
|
|
|
|
mutually_exclusive=[
|
|
|
|
('username', 'auth_token'),
|
|
|
|
],
|
2020-03-09 10:11:07 +01:00
|
|
|
supports_check_mode=False
|
|
|
|
)
|
|
|
|
|
|
|
|
category = module.params['category']
|
|
|
|
command_list = module.params['command']
|
|
|
|
|
|
|
|
# admin credentials used for authentication
|
|
|
|
creds = {'user': module.params['username'],
|
2021-03-19 21:14:33 +01:00
|
|
|
'pswd': module.params['password'],
|
|
|
|
'token': module.params['auth_token']}
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
# timeout
|
|
|
|
timeout = module.params['timeout']
|
|
|
|
|
|
|
|
# System, Manager or Chassis ID to modify
|
|
|
|
resource_id = module.params['resource_id']
|
|
|
|
|
|
|
|
# Build root URI
|
|
|
|
root_uri = "https://" + module.params['baseuri']
|
|
|
|
rf_utils = IdracRedfishUtils(creds, root_uri, timeout, module,
|
|
|
|
resource_id=resource_id, data_modification=True)
|
|
|
|
|
|
|
|
# Check that Category is valid
|
|
|
|
if category not in CATEGORY_COMMANDS_ALL:
|
2021-02-23 23:19:27 +01:00
|
|
|
module.fail_json(msg=to_native("Invalid Category '%s'. Valid Categories = %s" % (category, list(CATEGORY_COMMANDS_ALL.keys()))))
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
# Check that all commands are valid
|
|
|
|
for cmd in command_list:
|
|
|
|
# Fail if even one command given is invalid
|
|
|
|
if cmd not in CATEGORY_COMMANDS_ALL[category]:
|
|
|
|
module.fail_json(msg=to_native("Invalid Command '%s'. Valid Commands = %s" % (cmd, CATEGORY_COMMANDS_ALL[category])))
|
|
|
|
|
|
|
|
# Organize by Categories / Commands
|
|
|
|
|
|
|
|
if category == "Systems":
|
|
|
|
# execute only if we find a System resource
|
2023-04-20 06:59:52 +02:00
|
|
|
# NOTE: Currently overriding the usage of 'data_modification' due to
|
|
|
|
# how 'resource_id' is processed. In the case of CreateBiosConfigJob,
|
|
|
|
# we interact with BOTH systems and managers, so you currently cannot
|
|
|
|
# specify a single 'resource_id' to make both '_find_systems_resource'
|
|
|
|
# and '_find_managers_resource' return success. Since
|
|
|
|
# CreateBiosConfigJob doesn't use the matched 'resource_id' for a
|
|
|
|
# system regardless of what's specified, disabling the 'resource_id'
|
|
|
|
# inspection for the next call allows a specific manager to be
|
|
|
|
# specified with 'resource_id'. If we ever need to expand the input
|
|
|
|
# to inspect a specific system and manager in parallel, this will need
|
|
|
|
# updates.
|
|
|
|
rf_utils.data_modification = False
|
2020-03-09 10:11:07 +01:00
|
|
|
result = rf_utils._find_systems_resource()
|
2023-04-20 06:59:52 +02:00
|
|
|
rf_utils.data_modification = True
|
2020-03-09 10:11:07 +01:00
|
|
|
if result['ret'] is False:
|
|
|
|
module.fail_json(msg=to_native(result['msg']))
|
|
|
|
|
|
|
|
for command in command_list:
|
|
|
|
if command == "CreateBiosConfigJob":
|
|
|
|
# execute only if we find a Managers resource
|
|
|
|
result = rf_utils._find_managers_resource()
|
|
|
|
if result['ret'] is False:
|
|
|
|
module.fail_json(msg=to_native(result['msg']))
|
|
|
|
result = rf_utils.create_bios_config_job()
|
2023-04-19 20:11:57 +02:00
|
|
|
if 'job_id' in result:
|
|
|
|
return_values['job_id'] = result['job_id']
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
# Return data back or fail with proper message
|
|
|
|
if result['ret'] is True:
|
|
|
|
del result['ret']
|
2023-04-19 20:11:57 +02:00
|
|
|
module.exit_json(changed=True, msg='Action was successful', return_values=return_values)
|
2020-03-09 10:11:07 +01:00
|
|
|
else:
|
|
|
|
module.fail_json(msg=to_native(result['msg']))
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|