Skip to content

Commit

Permalink
Merged PR 56: irmc_setnextbootモジュールでbootsource='None'がエラーになるのを修正
Browse files Browse the repository at this point in the history
Related work items: #204
  • Loading branch information
kmok8oji committed Aug 29, 2024
2 parents 7d491f6 + 4ccfb9c commit 4318d69
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
8 changes: 4 additions & 4 deletions DOCUMENTATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -2132,8 +2132,8 @@ Default return values

| Parameter | Required | Default | Choices | Description |
|:----------|:---------|:--------|:--------|:----------- |
| bootmode | No | | Legacy<br/> UEFI<br/> | The mode for the next boot. |
| bootoverride | No | Once | Once<br/> Continuous<br/> | Boot override type. |
| bootmode | No | | Legacy<br/> UEFI<br/> | The mode for the next boot.<br/> If bootsource is 'None', it is ignored. |
| bootoverride | No | Once | Once<br/> Continuous<br/> | Boot override type.<br/> If bootsource is 'None', it is ignored. |
| bootsource | No | BiosSetup | None<br/> Pxe<br/> Floppy<br/> Cd<br/> Hdd<br/> BiosSetup<br/> | The source for the next boot. |
| irmc_password | Yes | | | Password for iRMC user for basic authentication. |
| irmc_url | Yes | | | IP address of the iRMC to be requested for data. |
Expand All @@ -2143,9 +2143,9 @@ Default return values
#### Examples

```yaml
# Set Bios to next boot from Virtual CD
# Set Bios to boot from the specified device.
# Note: boot from virtual CD might fail, if a 'real' DVD drive exists
- name: Set Bios to next boot from Virtual CD
- name: Set Bios to boot from the specified device.
irmc_setnextboot:
irmc_url: "{{ inventory_hostname }}"
irmc_username: "{{ irmc_user }}"
Expand Down
38 changes: 24 additions & 14 deletions library/irmc_setnextboot.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@
default: BiosSetup
choices: ['None', 'Pxe', 'Floppy', 'Cd', 'Hdd', 'BiosSetup']
bootoverride:
description: Boot override type.
description: Boot override type. If bootsource is 'None', it is ignored.
required: false
default: Once
choices: ['Once', 'Continuous']
bootmode:
description: The mode for the next boot.
description: The mode for the next boot. If bootsource is 'None', it is ignored.
required: false
choices: ['Legacy', 'UEFI']
'''
Expand All @@ -77,6 +77,9 @@


import json
from collections.abc import Iterable
from http import HTTPStatus
from typing import Any

from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.irmc import get_irmc_json, irmc_redfish_get, irmc_redfish_patch
Expand All @@ -96,7 +99,7 @@ def irmc_setnextboot(module: AnsibleModule) -> None:
status, sysdata, msg = irmc_redfish_get(module, 'redfish/v1/Systems/0/')
if status < 100:
module.fail_json(msg=msg, status=status, exception=sysdata)
elif status != 200:
elif status != HTTPStatus.OK:
module.fail_json(msg=msg, status=status)

# Evaluate function params against iRMC
Expand All @@ -115,20 +118,27 @@ def irmc_setnextboot(module: AnsibleModule) -> None:
result['status'] = 11
module.fail_json(**result)

# Set iRMC system data
body = {
'Boot': {
'BootSourceOverrideTarget': module.params['bootsource'],
'BootSourceOverrideEnabled': module.params['bootoverride'],
},
}
if module.params['bootmode'] is not None:
body['Boot']['BootSourceOverrideMode'] = module.params['bootmode']
etag = get_irmc_json(sysdata.json(), '@odata.etag')
def _create_boot_entries(bootsource: str, bootoverride: str, bootmode: str, **_: dict) -> Iterable[tuple[str, Any]]:
"""Create 'Boot' elements of request body for change boot source from `AnsibleModule.params`."""
yield 'BootSourceOverrideTarget', bootsource

# NOTE: If BootSourceOverrideTarget='None',
# BootSourceOverrideEnabled and BootSourceOverrideMode cannot be specified. (500 error occurs)
if bootsource == 'None':
return

yield 'BootSourceOverrideEnabled', bootoverride
if bootmode is not None:
yield 'BootSourceOverrideMode', bootmode

# Request body for change boot source
body = {'Boot': dict(_create_boot_entries(**module.params))}

etag = sysdata.json()['@odata.etag']
status, patch, msg = irmc_redfish_patch(module, 'redfish/v1/Systems/0/', json.dumps(body), etag)
if status < 100:
module.fail_json(msg=msg, status=status, exception=patch)
elif status != 200:
elif status != HTTPStatus.OK:
module.fail_json(msg=msg, status=status)

result['changed'] = True
Expand Down

0 comments on commit 4318d69

Please sign in to comment.