예제 #1
0
def main():
    result = {}
    module = AnsibleModule(
        argument_spec=dict(
            category=dict(required=True),
            command=dict(required=True, type='list'),
            baseuri=dict(required=True),
            username=dict(required=True),
            password=dict(required=True, no_log=True),
            id=dict(),
            new_username=dict(),
            new_password=dict(no_log=True),
            roleid=dict(),
            bootdevice=dict(),
            timeout=dict(type='int', default=10)
        ),
        supports_check_mode=False
    )

    category = module.params['category']
    command_list = module.params['command']

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

    # user to add/modify/delete
    user = {'userid': module.params['id'],
            'username': module.params['new_username'],
            'userpswd': module.params['new_password'],
            'userrole': module.params['roleid']}

    # timeout
    timeout = module.params['timeout']

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

    # Check that Category is valid
    if category not in CATEGORY_COMMANDS_ALL:
        module.fail_json(msg=to_native("Invalid Category '%s'. Valid Categories = %s" % (category, CATEGORY_COMMANDS_ALL.keys())))

    # 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 == "Accounts":
        ACCOUNTS_COMMANDS = {
            "AddUser": rf_utils.add_user,
            "EnableUser": rf_utils.enable_user,
            "DeleteUser": rf_utils.delete_user,
            "DisableUser": rf_utils.disable_user,
            "UpdateUserRole": rf_utils.update_user_role,
            "UpdateUserPassword": rf_utils.update_user_password
        }

        # execute only if we find an Account service resource
        result = rf_utils._find_accountservice_resource(rf_uri)
        if result['ret'] is False:
            module.fail_json(msg=to_native(result['msg']))

        for command in command_list:
            result = ACCOUNTS_COMMANDS[command](user)

    elif category == "Systems":
        # execute only if we find a System resource
        result = rf_utils._find_systems_resource(rf_uri)
        if result['ret'] is False:
            module.fail_json(msg=to_native(result['msg']))

        for command in command_list:
            if "Power" in command:
                result = rf_utils.manage_system_power(command)
            elif command == "SetOneTimeBoot":
                result = rf_utils.set_one_time_boot_device(module.params['bootdevice'])

    elif category == "Chassis":
        result = rf_utils._find_chassis_resource(rf_uri)
        if result['ret'] is False:
            module.fail_json(msg=to_native(result['msg']))

        led_commands = ["IndicatorLedOn", "IndicatorLedOff", "IndicatorLedBlink"]

        # Check if more than one led_command is present
        num_led_commands = sum([command in led_commands for command in command_list])
        if num_led_commands > 1:
            result = {'ret': False, 'msg': "Only one IndicatorLed command should be sent at a time."}
        else:
            for command in command_list:
                if command in led_commands:
                    result = rf_utils.manage_indicator_led(command)

    elif category == "Manager":
        MANAGER_COMMANDS = {
            "GracefulRestart": rf_utils.restart_manager_gracefully,
            "ClearLogs": rf_utils.clear_logs
        }

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

        for command in command_list:
            result = MANAGER_COMMANDS[command]()

    # Return data back or fail with proper message
    if result['ret'] is True:
        del result['ret']
        module.exit_json(changed=True, msg='Action was successful')
    else:
        module.fail_json(msg=to_native(result['msg']))
예제 #2
0
def main():
    result = {}
    module = AnsibleModule(argument_spec=dict(
        category=dict(required=True, type='str'),
        command=dict(required=True, type='str'),
        baseuri=dict(required=True, type='str'),
        user=dict(required=True, type='str'),
        password=dict(required=True, type='str', no_log=True),
        userid=dict(required=False, type='str'),
        username=dict(required=False, type='str'),
        userpswd=dict(required=False, type='str', no_log=True),
        userrole=dict(required=False, type='str'),
        bootdevice=dict(required=False, type='str'),
        mgr_attr_name=dict(required=False, type='str'),
        mgr_attr_value=dict(required=False, type='str'),
        bios_attr_name=dict(required=False, type='str'),
        bios_attr_value=dict(required=False, type='str'),
    ),
                           supports_check_mode=False)

    category = module.params['category']
    command = module.params['command']
    bootdevice = module.params['bootdevice']

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

    # user to add/modify/delete
    user = {
        'userid': module.params['userid'],
        'username': module.params['username'],
        'userpswd': module.params['userpswd'],
        'userrole': module.params['userrole']
    }

    # Manager attributes to update
    mgr_attributes = {
        'mgr_attr_name': module.params['mgr_attr_name'],
        'mgr_attr_value': module.params['mgr_attr_value']
    }
    # BIOS attributes to update
    bios_attributes = {
        'bios_attr_name': module.params['bios_attr_name'],
        'bios_attr_value': module.params['bios_attr_value']
    }

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

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

        # General
        if command == "GetSystemInventory":
            result = rf_utils.get_system_inventory()

        # Components
        elif command == "GetPsuInventory":
            result = rf_utils.get_psu_inventory()
        elif command == "GetCpuInventory":
            result = rf_utils.get_cpu_inventory()
        elif command == "GetNicInventory":
            result = rf_utils.get_nic_inventory()

        # Storage
        elif command == "GetStorageControllerInventory":
            result = rf_utils.get_storage_controller_inventory()
        elif command == "GetDiskInventory":
            result = rf_utils.get_disk_inventory()

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

        else:
            result = {'ret': False, 'msg': 'Invalid Command'}

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

        if command == "ListUsers":
            result = rf_utils.list_users(user)
        elif command == "AddUser":
            result = rf_utils.add_user(user)
        elif command == "EnableUser":
            result = rf_utils.enable_user(user)
        elif command == "DeleteUser":
            result = rf_utils.delete_user(user)
        elif command == "DisableUser":
            result = rf_utils.disable_user(user)
        elif command == "UpdateUserRole":
            result = rf_utils.update_user_role(user)
        elif command == "UpdateUserPassword":
            result = rf_utils.update_user_password(user)
        else:
            result = {'ret': False, 'msg': 'Invalid Command'}

    elif category == "System":
        # execute only if we find a System resource
        result = rf_utils._find_systems_resource(rf_uri)
        if result['ret'] is False:
            module.fail_json(msg=result['msg'])

        if command == "PowerOn" or command == "PowerForceOff" \
            or command == "PowerGracefulRestart" or command == "PowerGracefulShutdown":
            result = rf_utils.manage_system_power(command)
        elif command == "GetBiosAttributes":
            result = rf_utils.get_bios_attributes()
        elif command == "GetBiosBootOrder":
            result = rf_utils.get_bios_boot_order()
        elif command == "SetOneTimeBoot":
            result = rf_utils.set_one_time_boot_device(bootdevice)
        elif command == "SetBiosDefaultSettings":
            result = rf_utils.set_bios_default_settings()
        elif command == "SetBiosAttributes":
            result = rf_utils.set_bios_attributes(bios_attributes)
        elif command == "CreateBiosConfigJob":
            # execute only if we find a Managers resource
            result = rf_utils._find_managers_resource(rf_uri)
            if result['ret'] is False:
                module.fail_json(msg=result['msg'])
            result = rf_utils.create_bios_config_job()
        else:
            result = {'ret': False, 'msg': 'Invalid Command'}

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

        if command == "GetFirmwareInventory":
            result = rf_utils.get_firmware_inventory()
        else:
            result = {'ret': False, 'msg': 'Invalid Command'}

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

        if command == "GracefulRestart":
            result = rf_utils.restart_manager_gracefully()
        elif command == "GetAttributes":
            result = rf_utils.get_manager_attributes()
        elif command == "SetAttributes":
            result = rf_utils.set_manager_attributes(mgr_attributes)

        # Logs
        elif command == "GetLogs":
            result = rf_utils.get_logs()
        elif command == "ClearLogs":
            result = rf_utils.clear_logs()
        else:
            result = {'ret': False, 'msg': 'Invalid Command'}

    else:
        result = {'ret': False, 'msg': 'Invalid Category'}

    # Return data back or fail with proper message
    if result['ret'] is True:
        del result['ret']
        module.exit_json(result=result)
    else:
        module.fail_json(msg=result['msg'])
예제 #3
0
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),
                                              timeout=dict(type='int',
                                                           default=10)),
                           supports_check_mode=False)

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

    # timeout
    timeout = module.params['timeout']

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

    # 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_multi_system_inventory()
                elif command == "GetCpuInventory":
                    result["cpu"] = rf_utils.get_multi_cpu_inventory()
                elif command == "GetMemoryInventory":
                    result["memory"] = rf_utils.get_multi_memory_inventory()
                elif command == "GetNicInventory":
                    result["nic"] = rf_utils.get_multi_nic_inventory(category)
                elif command == "GetStorageControllerInventory":
                    result[
                        "storage_controller"] = rf_utils.get_multi_storage_controller_inventory(
                        )
                elif command == "GetDiskInventory":
                    result["disk"] = rf_utils.get_multi_disk_inventory()
                elif command == "GetBiosAttributes":
                    result[
                        "bios_attribute"] = rf_utils.get_multi_bios_attributes(
                        )
                elif command == "GetBootOrder":
                    result["boot_order"] = rf_utils.get_multi_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 command == "GetPsuInventory":
                    result["psu"] = rf_utils.get_psu_inventory()
                elif command == "GetChassisThermals":
                    result["thermals"] = rf_utils.get_chassis_thermals()

        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 command == "GetFirmwareUpdateCapabilities":
                    result[
                        "firmware_update_capabilities"] = rf_utils.get_firmware_update_capabilities(
                        )

        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_multi_nic_inventory(
                        category)
                elif command == "GetLogs":
                    result["log"] = rf_utils.get_logs()

    # Return data back
    module.exit_json(ansible_facts=dict(redfish_facts=result))
def main():
    result = {}
    module = AnsibleModule(argument_spec=dict(
        category=dict(required=True, type='str', default=None),
        command=dict(required=True, type='str', default=None),
        baseuri=dict(required=True, type='str', default=None),
        user=dict(required=False, type='str', default='root'),
        password=dict(required=False,
                      type='str',
                      default='calvin',
                      no_log=True),
        userid=dict(required=False, type='str', default=None),
        username=dict(required=False, type='str', default=None),
        userpswd=dict(required=False, type='str', default=None, no_log=True),
        userrole=dict(required=False, type='str', default=None),
        bootdevice=dict(required=False, type='str', default=None),
        mgr_attr_name=dict(required=False, type='str', default=None),
        mgr_attr_value=dict(required=False, type='str', default=None),
        bios_attr_name=dict(required=False, type='str', default=None),
        bios_attr_value=dict(required=False, type='str', default=None),
    ),
                           supports_check_mode=False)

    # Disable insecure-certificate-warning message
    requests.packages.urllib3.disable_warnings(InsecureRequestWarning)

    category = module.params['category']
    command = module.params['command']
    bootdevice = module.params['bootdevice']

    # admin credentials used for authentication
    creds = {'user': module.params['user'], 'pswd': module.params['password']}
    # user to add/modify/delete
    user = {
        'userid': module.params['userid'],
        'username': module.params['username'],
        'userpswd': module.params['userpswd'],
        'userrole': module.params['userrole']
    }
    # Manager attributes to update
    mgr_attributes = {
        'mgr_attr_name': module.params['mgr_attr_name'],
        'mgr_attr_value': module.params['mgr_attr_value']
    }
    # BIOS attributes to update
    bios_attributes = {
        'bios_attr_name': module.params['bios_attr_name'],
        'bios_attr_value': module.params['bios_attr_value']
    }

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

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

        # General
        if command == "GetSystemInventory":
            result = rf_utils.get_system_inventory()

        # Components
        elif command == "GetPsuInventory":
            result = rf_utils.get_psu_inventory()
        elif command == "GetCpuInventory":
            result = rf_utils.get_cpu_inventory("/Processors")
        elif command == "GetNicInventory":
            result = rf_utils.get_nic_inventory("/EthernetInterfaces")

        # Storage
        elif command == "GetStorageControllerInventory":
            result = rf_utils.get_storage_controller_info()
        elif command == "GetDiskInventory":
            result = rf_utils.get_disk_info()

        # Chassis
        elif command == "GetFanInventory":
            # execute only if we find Chassis resource
            result = rf_utils._find_chassis_resource(rf_uri)
            if result['ret'] == False: module.fail_json(msg=result['msg'])
            result = rf_utils.get_fan_inventory("/Thermal")

        else:
            result = {'ret': False, 'msg': 'Invalid Command'}

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

        if command == "ListUsers":
            result = rf_utils.list_users(user)
        elif command == "AddUser":
            result = rf_utils.add_user(user)
        elif command == "EnableUser":
            result = rf_utils.enable_user(user)
        elif command == "DeleteUser":
            result = rf_utils.delete_user(user)
        elif command == "DisableUser":
            result = rf_utils.disable_user(user)
        elif command == "UpdateUserRole":
            result = rf_utils.update_user_role(user)
        elif command == "UpdateUserPassword":
            result = rf_utils.update_user_password(user)
        else:
            result = {'ret': False, 'msg': 'Invalid Command'}

    elif category == "System":
        # execute only if we find a System resource
        result = rf_utils._find_systems_resource(rf_uri)
        if result['ret'] == False: module.fail_json(msg=result['msg'])

        if command == "PowerOn" or command == "PowerForceOff" or command == "PowerGracefulRestart" or command == "PowerGracefulShutdown":
            result = rf_utils.manage_system_power(
                "/Actions/ComputerSystem.Reset", command)
        elif command == "GetBiosAttributes":
            result = rf_utils.get_bios_attributes("/Bios")
        elif command == "GetBiosBootOrder":
            result = rf_utils.get_bios_boot_order("/Bios", "/BootSources")
        elif command == "SetOneTimeBoot":
            result = rf_utils.set_one_time_boot_device(bootdevice)
        elif command == "SetBiosDefaultSettings":
            result = rf_utils.set_bios_default_settings(
                "/Bios/Actions/Bios.ResetBios")
        elif command == "SetBiosAttributes":
            result = rf_utils.set_bios_attributes("/Bios/Settings",
                                                  bios_attributes)
        elif command == "CreateBiosConfigJob":
            # execute only if we find a Managers resource
            result = rf_utils._find_managers_resource(rf_uri)
            if result['ret'] == False: module.fail_json(msg=result['msg'])
            result = rf_utils.create_bios_config_job("/Bios/Settings", "/Jobs")
        else:
            result = {'ret': False, 'msg': 'Invalid Command'}

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

        if command == "GetFirmwareInventory":
            result = rf_utils.get_firmware_inventory()
        else:
            result = {'ret': False, 'msg': 'Invalid Command'}

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

        # The URIs being passed are hard-coded but could easily be built dynamically by
        # looking in resources Links["Oem"] and Actions["Oem"]["target"] under manager_uri.
        # Leaving here for reference only so it can be adapted for other OEMs.
        if command == "GracefulRestart":
            result = rf_utils.restart_manager_gracefully(
                "/Actions/Manager.Reset")
        elif command == "GetAttributes":
            result = rf_utils.get_manager_attributes("/Attributes")
        elif command == "SetAttributes":
            result = rf_utils.set_manager_attributes("/Attributes",
                                                     mgr_attributes)
        elif command == "SetDefaultSettings":
            result = rf_utils.set_manager_default_settings(
                "/Actions/Oem/DellManager.ResetToDefaults")

        # Logs
        elif command == "ViewLogs":
            result = rf_utils.view_logs()
        elif command == "ClearLogs":
            result = rf_utils.clear_logs()
        else:
            result = {'ret': False, 'msg': 'Invalid Command'}

    else:
        result = {'ret': False, 'msg': 'Invalid Category'}

    # Return data back or fail with proper message
    if result['ret'] == True:
        del result['ret']  # Don't want to pass this back
        module.exit_json(result=result)
    else:
        module.fail_json(msg=result['msg'])