# Buggy code
def main():
    result = {}
    resource = {}
    category_list = []
    module = AnsibleModule(
        argument_spec=dict(
            category=dict(type="list", default=["Systems"]),
            command=dict(type="list"),
            baseuri=dict(required=True),
            username=dict(required=True),
            password=dict(required=True, no_log=True),
        ),
        supports_check_mode=False,
    )

    # admin credentials used for authentication
    creds = {"user": module.params["username"], "pswd": module.params["password"]}

    # Build root URI
    root_uri = "https://" + module.params["baseuri"]
    rf_uri = "/redfish/v1/"
    rf_utils = RedfishUtils(creds, root_uri)

    # Build Category list
    if "all" in module.params["category"]:
        for entry in CATEGORY_COMMANDS_ALL:
            category_list.append(entry)
    else:
        # one or more categories specified
        category_list = module.params["category"]

    for category in category_list:
        command_list = []
        # Build Command list for each Category
        if category in CATEGORY_COMMANDS_ALL:
            if not module.params["command"]:
                # True if we don't specify a command --> use default
                command_list.append(CATEGORY_COMMANDS_DEFAULT[category])
            elif "all" in module.params["command"]:
                for entry in range(len(CATEGORY_COMMANDS_ALL[category])):
                    command_list.append(CATEGORY_COMMANDS_ALL[category][entry])
            # one or more commands
            else:
                command_list = module.params["command"]
                # Verify 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="Invalid Command: %s" % cmd)
        else:
            # Fail if even one category given is invalid
            module.fail_json(msg="Invalid Category: %s" % category)

        # Organize by Categories / Commands
        if category == "Systems":
            # execute only if we find a Systems resource
            resource = rf_utils._find_systems_resource(rf_uri)
            if resource["ret"] is False:
                module.fail_json(msg=resource["msg"])

            for command in command_list:
                if command == "GetSystemInventory":
                    result["system"] = rf_utils.get_system_inventory()
                elif command == "GetPsuInventory":
                    result["psu"] = rf_utils.get_psu_inventory()
                elif command == "GetCpuInventory":
                    result["cpu"] = rf_utils.get_cpu_inventory()
                elif command == "GetNicInventory":
                    result["nic"] = rf_utils.get_nic_inventory(category)
                elif command == "GetStorageControllerInventory":
                    result["storage_controller"] = (
                        rf_utils.get_storage_controller_inventory()
                    )
                elif command == "GetDiskInventory":
                    result["disk"] = rf_utils.get_disk_inventory()
                elif command == "GetBiosAttributes":
                    result["bios_attribute"] = rf_utils.get_bios_attributes()
                elif command == "GetBiosBootOrder":
                    result["bios_boot_order"] = rf_utils.get_bios_boot_order()

        elif category == "Chassis":
            # execute only if we find Chassis resource
            resource = rf_utils._find_chassis_resource(rf_uri)
            if resource["ret"] is False:
                module.fail_json(msg=resource["msg"])

            for command in command_list:
                if command == "GetFanInventory":
                    result["fan"] = rf_utils.get_fan_inventory()

        elif category == "Accounts":
            # execute only if we find an Account service resource
            resource = rf_utils._find_accountservice_resource(rf_uri)
            if resource["ret"] is False:
                module.fail_json(msg=resource["msg"])

            for command in command_list:
                if command == "ListUsers":
                    result["user"] = rf_utils.list_users()

        elif category == "Update":
            # execute only if we find UpdateService resources
            resource = rf_utils._find_updateservice_resource(rf_uri)
            if resource["ret"] is False:
                module.fail_json(msg=resource["msg"])

            for command in command_list:
                if command == "GetFirmwareInventory":
                    result["firmware"] = rf_utils.get_firmware_inventory()

        elif category == "Manager":
            # execute only if we find a Manager service resource
            resource = rf_utils._find_managers_resource(rf_uri)
            if resource["ret"] is False:
                module.fail_json(msg=resource["msg"])

            for command in command_list:
                if command == "GetManagerNicInventory":
                    result["manager_nics"] = rf_utils.get_nic_inventory(
                        resource_type=category
                    )
                elif command == "GetLogs":
                    result["log"] = rf_utils.get_logs()

    # Return data back
    module.exit_json(ansible_facts=dict(redfish_facts=result))


# Traceback (KeyError)
redfish$ ansible/bin/ansible-playbook test_redfish.yml -vvvv

ansible-playbook 2.8.0.dev0 (devel 518f80c3fb) last updated 2018/10/11 16:25:06 (GMT -400)
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/redfish/ansible/lib/ansible
executable location = ansible/bin/ansible-playbook
python version = 2.7.12 (default, Dec  4 2017, 14:50:18) [GCC 5.4.0 20160609]
No config file found; using defaults
setting up inventory plugins
[WARNING]: Unable to parse /etc/ansible/hosts as an inventory source

[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/redfish/ansible/lib/ansible/plugins/callback/default.pyc

PLAYBOOK: test_redfish.yml *************************************************************************************************************************1 plays in test_redfish.yml

PLAY [Test Redfish modules!] ***********************************************************************************************************************META: ran handlers

TASK [Get all information available in System category] ********************************************************************************************task path: /mnt/c/Users/amadsen/Coding/redfish/test_redfish.yml:12
<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-1540398196.31-202659559090196 `" &amp;&amp; echo ansible-tmp-1540398196.31-202659559090196="` echo /home/xander/.ansible/tmp/ansible-tmp-1540398196.31-202659559090196 `" ) &amp;&amp; sleep 0'
Using module file /mnt/c/Users/amadsen/Coding/redfish/ansible/lib/ansible/modules/remote_management/redfish/redfish_facts.py
<127.0.0.1> PUT /home/xander/.ansible/tmp/ansible-local-206504PW1hZ/tmp25SJAQ TO /home/xander/.ansible/tmp/ansible-tmp-1540398196.31-202659559090196/AnsiballZ_redfish_facts.py
<127.0.0.1> EXEC /bin/sh -c 'chmod u+x /home/xander/.ansible/tmp/ansible-tmp-1540398196.31-202659559090196/ /home/xander/.ansible/tmp/ansible-tmp-1540398196.31-202659559090196/AnsiballZ_redfish_facts.py &amp;&amp; sleep 0'
<127.0.0.1> EXEC /bin/sh -c '/usr/bin/python /home/xander/.ansible/tmp/ansible-tmp-1540398196.31-202659559090196/AnsiballZ_redfish_facts.py &amp;&amp; sleep 0'
<127.0.0.1> EXEC /bin/sh -c 'rm -f -r /home/xander/.ansible/tmp/ansible-tmp-1540398196.31-202659559090196/ > /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-1540398196.31-202659559090196/AnsiballZ_redfish_facts.py", line 113, in <module>
_ansiballz_main()
File "/home/xander/.ansible/tmp/ansible-tmp-1540398196.31-202659559090196/AnsiballZ_redfish_facts.py", line 105, in _ansiballz_main
invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)
File "/home/xander/.ansible/tmp/ansible-tmp-1540398196.31-202659559090196/AnsiballZ_redfish_facts.py", line 48, in invoke_module
imp.load_module('__main__', mod, module, MOD_DESC)
File "/tmp/ansible_redfish_facts_payload_q2Fkwl/__main__.py", line 267, in <module>
File "/tmp/ansible_redfish_facts_payload_q2Fkwl/__main__.py", line 218, in main
File "/tmp/ansible_redfish_facts_payload_q2Fkwl/ansible_redfish_facts_payload.zip/ansible/module_utils/redfish_utils.py", line 557, in get_bios_boot_order
KeyError: 'BootMode'

failed: [localhost] (item=GetBiosBootOrder) => {
"changed": false,
"item": "GetBiosBootOrder",
"module_stderr": "Traceback (most recent call last):\n  File \"/home/xander/.ansible/tmp/ansible-tmp-1540398196.31-202659559090196/AnsiballZ_redfish_facts.py\", line 113, in <module>\n    _ansiballz_main()\n  File \"/home/xander/.ansible/tmp/ansible-tmp-1540398196.31-202659559090196/AnsiballZ_redfish_facts.py\", line 105, in _ansiballz_main\n    invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)\n  File \"/home/xander/.ansible/tmp/ansible-tmp-1540398196.31-202659559090196/AnsiballZ_redfish_facts.py\", line 48, in invoke_module\n    imp.load_module('__main__', mod, module, MOD_DESC)\n  File \"/tmp/ansible_redfish_facts_payload_q2Fkwl/__main__.py\", line 267, in <module>\n  File \"/tmp/ansible_redfish_facts_payload_q2Fkwl/__main__.py\", line 218, in main\n  File \"/tmp/ansible_redfish_facts_payload_q2Fkwl/ansible_redfish_facts_payload.zip/ansible/module_utils/redfish_utils.py\", line 557, in get_bios_boot_order\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/redfish/test_redfish.retry

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

# Fixed code
def main():
    result = {}
    resource = {}
    category_list = []
    module = AnsibleModule(
        argument_spec=dict(
            category=dict(type="list", default=["Systems"]),
            command=dict(type="list"),
            baseuri=dict(required=True),
            username=dict(required=True),
            password=dict(required=True, no_log=True),
        ),
        supports_check_mode=False,
    )

    # admin credentials used for authentication
    creds = {"user": module.params["username"], "pswd": module.params["password"]}

    # Build root URI
    root_uri = "https://" + module.params["baseuri"]
    rf_uri = "/redfish/v1/"
    rf_utils = RedfishUtils(creds, root_uri)

    # Build Category list
    if "all" in module.params["category"]:
        for entry in CATEGORY_COMMANDS_ALL:
            category_list.append(entry)
    else:
        # one or more categories specified
        category_list = module.params["category"]

    for category in category_list:
        command_list = []
        # Build Command list for each Category
        if category in CATEGORY_COMMANDS_ALL:
            if not module.params["command"]:
                # True if we don't specify a command --> use default
                command_list.append(CATEGORY_COMMANDS_DEFAULT[category])
            elif "all" in module.params["command"]:
                for entry in range(len(CATEGORY_COMMANDS_ALL[category])):
                    command_list.append(CATEGORY_COMMANDS_ALL[category][entry])
            # one or more commands
            else:
                command_list = module.params["command"]
                # Verify 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="Invalid Command: %s" % cmd)
        else:
            # Fail if even one category given is invalid
            module.fail_json(msg="Invalid Category: %s" % category)

        # Organize by Categories / Commands
        if category == "Systems":
            # execute only if we find a Systems resource
            resource = rf_utils._find_systems_resource(rf_uri)
            if resource["ret"] is False:
                module.fail_json(msg=resource["msg"])

            for command in command_list:
                if command == "GetSystemInventory":
                    result["system"] = rf_utils.get_system_inventory()
                elif command == "GetPsuInventory":
                    result["psu"] = rf_utils.get_psu_inventory()
                elif command == "GetCpuInventory":
                    result["cpu"] = rf_utils.get_cpu_inventory()
                elif command == "GetNicInventory":
                    result["nic"] = rf_utils.get_nic_inventory(category)
                elif command == "GetStorageControllerInventory":
                    result["storage_controller"] = (
                        rf_utils.get_storage_controller_inventory()
                    )
                elif command == "GetDiskInventory":
                    result["disk"] = rf_utils.get_disk_inventory()
                elif command == "GetBiosAttributes":
                    result["bios_attribute"] = rf_utils.get_bios_attributes()
                elif command == "GetBootOrder":
                    result["boot_order"] = rf_utils.get_boot_order()

        elif category == "Chassis":
            # execute only if we find Chassis resource
            resource = rf_utils._find_chassis_resource(rf_uri)
            if resource["ret"] is False:
                module.fail_json(msg=resource["msg"])

            for command in command_list:
                if command == "GetFanInventory":
                    result["fan"] = rf_utils.get_fan_inventory()

        elif category == "Accounts":
            # execute only if we find an Account service resource
            resource = rf_utils._find_accountservice_resource(rf_uri)
            if resource["ret"] is False:
                module.fail_json(msg=resource["msg"])

            for command in command_list:
                if command == "ListUsers":
                    result["user"] = rf_utils.list_users()

        elif category == "Update":
            # execute only if we find UpdateService resources
            resource = rf_utils._find_updateservice_resource(rf_uri)
            if resource["ret"] is False:
                module.fail_json(msg=resource["msg"])

            for command in command_list:
                if command == "GetFirmwareInventory":
                    result["firmware"] = rf_utils.get_firmware_inventory()

        elif category == "Manager":
            # execute only if we find a Manager service resource
            resource = rf_utils._find_managers_resource(rf_uri)
            if resource["ret"] is False:
                module.fail_json(msg=resource["msg"])

            for command in command_list:
                if command == "GetManagerNicInventory":
                    result["manager_nics"] = rf_utils.get_nic_inventory(
                        resource_type=category
                    )
                elif command == "GetLogs":
                    result["log"] = rf_utils.get_logs()

    # Return data back
    module.exit_json(ansible_facts=dict(redfish_facts=result))