# Buggy code
def set_one_time_boot_device(self, bootdevice):
    result = {}
    key = "Bios"

    # 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"]

    response = self.get_request(self.root_uri + bios_uri)
    if response["ret"] is False:
        return response
    data = response["data"]

    boot_mode = data["Attributes"]["BootMode"]
    if boot_mode == "Uefi":
        payload = {
            "Boot": {
                "BootSourceOverrideTarget": "UefiTarget",
                "UefiTargetBootSourceOverride": bootdevice,
            }
        }
    else:
        payload = {"Boot": {"BootSourceOverrideTarget": bootdevice}}

    response = self.patch_request(self.root_uri + self.systems_uris[0], payload)
    if response["ret"] is False:
        return response
    return {"ret": True}


# Traceback (KeyError)
$ bin/ansible-playbook ../test_redfish_command.yml  -vvvv
ansible-playbook 2.8.0.dev0
config file = None
configured module search path = [u'/home/xander/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /mnt/c/Users/amadsen/Coding/lenovo-redfish-automated-testing/ansible/lib/ansible
executable location = bin/ansible-playbook
python version = 2.7.12 (default, Nov 12 2018, 14:36:49) [GCC 5.4.0 20160609]
No config file found; using defaults
setting up inventory plugins
host_list declined parsing /etc/ansible/hosts as it did not pass it's verify_file() method
Skipping due to inventory source not existing or not being readable by the current user
script declined parsing /etc/ansible/hosts as it did not pass it's verify_file() method
auto declined parsing /etc/ansible/hosts as it did not pass it's verify_file() method
Skipping due to inventory source not existing or not being readable by the current user
yaml declined parsing /etc/ansible/hosts as it did not pass it's verify_file() method
Skipping due to inventory source not existing or not being readable by the current user
ini declined parsing /etc/ansible/hosts as it did not pass it's verify_file() method
Skipping due to inventory source not existing or not being readable by the current user
toml declined parsing /etc/ansible/hosts as it did not pass it's verify_file() method
[WARNING]: No inventory was parsed, only implicit localhost is available

[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does
not match 'all'

Loading callback plugin default of type stdout, v2.0 from /mnt/c/Users/amadsen/Coding/lenovo-redfish-automated-testing/ansible/lib/ansible/plugins/callback/default.pyc

PLAYBOOK: test_redfish_command.yml ***************************************************************************Positional arguments: ../test_redfish_command.yml
become_user: root
become_method: sudo
inventory: (u'/etc/ansible/hosts',)
tags: (u'all',)
forks: 5
verbosity: 4
connection: smart
timeout: 10
1 plays in ../test_redfish_command.yml

PLAY [localhost] *********************************************************************************************META: ran handlers

TASK [Set one time boot option] ******************************************************************************task path: /mnt/c/Users/amadsen/Coding/lenovo-redfish-automated-testing/test_redfish_command.yml:9
<127.0.0.1> ESTABLISH LOCAL CONNECTION FOR USER: xander
<127.0.0.1> EXEC /bin/sh -c 'echo ~xander &amp;&amp; sleep 0'
<127.0.0.1> EXEC /bin/sh -c '( umask 77 &amp;&amp; mkdir -p "` echo /home/xander/.ansible/tmp/ansible-tmp-1549463086.08-258158793933588 `" &amp;&amp; echo ansible-tmp-1549463086.08-258158793933588="` echo /home/xander/.ansible/tmp/ansible-tmp-1549463086.08-258158793933588 `" ) &amp;&amp; sleep 0'
Using module file /mnt/c/Users/amadsen/Coding/lenovo-redfish-automated-testing/ansible/lib/ansible/modules/remote_management/redfish/redfish_command.py
<127.0.0.1> PUT /home/xander/.ansible/tmp/ansible-local-1602jVNR9M/tmpEuc2cS TO /home/xander/.ansible/tmp/ansible-tmp-1549463086.08-258158793933588/AnsiballZ_redfish_command.py
<127.0.0.1> EXEC /bin/sh -c 'chmod u+x /home/xander/.ansible/tmp/ansible-tmp-1549463086.08-258158793933588/ /home/xander/.ansible/tmp/ansible-tmp-1549463086.08-258158793933588/AnsiballZ_redfish_command.py &amp;&amp; sleep 0'
<127.0.0.1> EXEC /bin/sh -c '/usr/bin/python /home/xander/.ansible/tmp/ansible-tmp-1549463086.08-258158793933588/AnsiballZ_redfish_command.py &amp;&amp; sleep 0'
<127.0.0.1> EXEC /bin/sh -c 'rm -f -r /home/xander/.ansible/tmp/ansible-tmp-1549463086.08-258158793933588/ > /dev/null 2>&amp;1 &amp;&amp; sleep 0'
The full traceback is:
Traceback (most recent call last):
File "/home/xander/.ansible/tmp/ansible-tmp-1549463086.08-258158793933588/AnsiballZ_redfish_command.py", line 113, in <module>
_ansiballz_main()
File "/home/xander/.ansible/tmp/ansible-tmp-1549463086.08-258158793933588/AnsiballZ_redfish_command.py", line 105, in _ansiballz_main
invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)
File "/home/xander/.ansible/tmp/ansible-tmp-1549463086.08-258158793933588/AnsiballZ_redfish_command.py", line 48, in invoke_module
imp.load_module('__main__', mod, module, MOD_DESC)
File "/tmp/ansible_redfish_command_payload_yaYKzO/__main__.py", line 256, in <module>
File "/tmp/ansible_redfish_command_payload_yaYKzO/__main__.py", line 231, in main
File "/tmp/ansible_redfish_command_payload_yaYKzO/ansible_redfish_command_payload.zip/ansible/module_utils/redfish_utils.py", line 679, in set_one_time_boot_device
KeyError: 'BootMode'

fatal: [localhost]: FAILED! => {
"changed": false,
"module_stderr": "Traceback (most recent call last):\n  File \"/home/xander/.ansible/tmp/ansible-tmp-1549463086.08-258158793933588/AnsiballZ_redfish_command.py\", line 113, in <module>\n    _ansiballz_main()\n  File
\"/home/xander/.ansible/tmp/ansible-tmp-1549463086.08-258158793933588/AnsiballZ_redfish_command.py\", line 105, in _ansiballz_main\n    invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)\n  File \"/home/xander/.ansible/tmp/ansible-tmp-1549463086.08-258158793933588/AnsiballZ_redfish_command.py\", line 48, in invoke_module\n
imp.load_module('__main__', mod, module, MOD_DESC)\n  File \"/tmp/ansible_redfish_command_payload_yaYKzO/__main__.py\", line 256, in <module>\n  File \"/tmp/ansible_redfish_command_payload_yaYKzO/__main__.py\", line 231, in main\n  File \"/tmp/ansible_redfish_command_payload_yaYKzO/ansible_redfish_command_payload.zip/ansible/module_utils/redfish_utils.py\", line 679, in set_one_time_boot_device\nKeyError: 'BootMode'\n",
"module_stdout": "",
"msg": "MODULE FAILURE\nSee stdout/stderr for the exact error",
"rc": 1
}
to retry, use: --limit @/mnt/c/Users/amadsen/Coding/lenovo-redfish-automated-testing/test_redfish_command.retry

PLAY RECAP ***************************************************************************************************localhost                  : ok=0    changed=0    unreachable=0    failed=1    skipped=0

# Fixed code
def set_one_time_boot_device(self, bootdevice, uefi_target, boot_next):
    result = {}
    key = "Boot"

    if not bootdevice:
        return {"ret": False, "msg": "bootdevice option required for SetOneTimeBoot"}

    # 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}

    boot = data[key]

    annotation = "BootSourceOverrideTarget@Redfish.AllowableValues"
    if annotation in boot:
        allowable_values = boot[annotation]
        if isinstance(allowable_values, list) and bootdevice not in allowable_values:
            return {
                "ret": False,
                "msg": "Boot device %s not in list of allowable values (%s)"
                % (bootdevice, allowable_values),
            }

    # read existing values
    enabled = boot.get("BootSourceOverrideEnabled")
    target = boot.get("BootSourceOverrideTarget")
    cur_uefi_target = boot.get("UefiTargetBootSourceOverride")
    cur_boot_next = boot.get("BootNext")

    if bootdevice == "UefiTarget":
        if not uefi_target:
            return {
                "ret": False,
                "msg": "uefi_target option required to SetOneTimeBoot for UefiTarget",
            }
        if (
            enabled == "Once"
            and target == bootdevice
            and uefi_target == cur_uefi_target
        ):
            # If properties are already set, no changes needed
            return {"ret": True, "changed": False}
        payload = {
            "Boot": {
                "BootSourceOverrideEnabled": "Once",
                "BootSourceOverrideTarget": bootdevice,
                "UefiTargetBootSourceOverride": uefi_target,
            }
        }
    elif bootdevice == "UefiBootNext":
        if not boot_next:
            return {
                "ret": False,
                "msg": "boot_next option required to SetOneTimeBoot for UefiBootNext",
            }
        if enabled == "Once" and target == bootdevice and boot_next == cur_boot_next:
            # If properties are already set, no changes needed
            return {"ret": True, "changed": False}
        payload = {
            "Boot": {
                "BootSourceOverrideEnabled": "Once",
                "BootSourceOverrideTarget": bootdevice,
                "BootNext": boot_next,
            }
        }
    else:
        if enabled == "Once" and target == bootdevice:
            # If properties are already set, no changes needed
            return {"ret": True, "changed": False}
        payload = {
            "Boot": {
                "BootSourceOverrideEnabled": "Once",
                "BootSourceOverrideTarget": bootdevice,
            }
        }

    response = self.patch_request(self.root_uri + self.systems_uris[0], payload)
    if response["ret"] is False:
        return response
    return {"ret": True, "changed": True}