Exemplo n.º 1
0
def main():
    module_args = dict(config_name=dict(type='str', default='running-config'),
                       output_file=dict(type='str', default=None),
                       remote_output_file_tftp_path=dict(type='str',
                                                         default=None),
                       config_type=dict(type='str',
                                        default='json',
                                        choices=['json', 'cli']),
                       vrf=dict(type='str'),
                       sort_json=dict(type='bool', default=True))

    # Version management
    try:

        from ansible.module_utils.aoscx_pyaoscx import Session
        from pyaoscx.session import Session as Pyaoscx_Session
        from pyaoscx.device import Device

        USE_PYAOSCX_SDK = True

    except ImportError:
        USE_PYAOSCX_SDK = False

    # Use PYAOSCX SDK
    if USE_PYAOSCX_SDK:
        from ansible.module_utils.basic import AnsibleModule

        # ArubaModule
        ansible_module = AnsibleModule(argument_spec=module_args,
                                       supports_check_mode=True)

        # Session
        session = Session(ansible_module)

        # Set Variables
        tftp_path = \
            ansible_module.params['remote_output_file_tftp_path']
        vrf = ansible_module.params['vrf']
        config_name = ansible_module.params['config_name']
        config_type = ansible_module.params['config_type']
        config_file = ansible_module.params['output_file']

        result = dict(changed=False)

        if ansible_module.check_mode:
            ansible_module.exit_json(**result)

        # Get session serialized information
        session_info = session.get_session()
        # Create pyaoscx.session object
        s = Pyaoscx_Session.from_session(session_info['s'],
                                         session_info['url'])

        # Create a Device Object
        device = Device(s)

        # Create a Configuration Object
        config = device.configuration()
        success = config.backup_configuration(config_name=config_name,
                                              output_file=config_file,
                                              vrf=vrf,
                                              config_type=config_type,
                                              remote_file_tftp_path=tftp_path)

        # Changed
        result['changed'] = success

        # Exit
        ansible_module.exit_json(**result)

    # Use Older version
    else:

        aruba_ansible_module = ArubaAnsibleModule(module_args=module_args)

        tftp_path = \
            aruba_ansible_module.module.params['remote_output_file_tftp_path']
        vrf = aruba_ansible_module.module.params['vrf']
        config_name = aruba_ansible_module.module.params['config_name']
        config_type = aruba_ansible_module.module.params['config_type']
        config_file = aruba_ansible_module.module.params['output_file']

        if tftp_path is not None:
            if vrf is None:
                aruba_ansible_module.module.fail_json(
                    msg="VRF needs to be provided in order to TFTP "
                    "the configuration from the switch")
            tftp_path_replace = tftp_path.replace("/", "%2F")
            tftp_path_encoded = tftp_path_replace.replace(":", "%3A")
            if config_name != 'running-config' and config_name != 'startup-config':
                aruba_ansible_module.module.fail_json(
                    msg="Only running-config or "
                    "startup-config can be backed-up using TFTP")
            aruba_ansible_module.copy_switch_config_to_remote_location(
                config_name, config_type, tftp_path_encoded, vrf)
        else:

            config_json = aruba_ansible_module.get_switch_config(
                store_config=False)
            with open(config_file, 'w') as to_file:
                formatted_file = json.dumps(config_json, indent=4)
                to_file.write(formatted_file)

        result = dict(changed=aruba_ansible_module.changed,
                      warnings=aruba_ansible_module.warnings)
        result["changed"] = True
        aruba_ansible_module.module.exit_json(**result)
Exemplo n.º 2
0
def main():
    module_args = dict(
        source_config=dict(type='str', default='running-config'),
        destination_config=dict(type='str', default='startup-config')
    )

    # Version management
    try:

        from ansible.module_utils.aoscx_pyaoscx import Session
        from pyaoscx.session import Session as Pyaoscx_Session
        from pyaoscx.device import Device

        USE_PYAOSCX_SDK = True

    except ImportError:
        USE_PYAOSCX_SDK = False

    # Use PYAOSCX SDK
    if USE_PYAOSCX_SDK:
        from ansible.module_utils.basic import AnsibleModule

        # ArubaModule
        ansible_module = AnsibleModule(
            argument_spec=module_args,
            supports_check_mode=True)

        # Session
        session = Session(ansible_module)

        # Set Variables
        source_config = ansible_module.params['source_config']
        destination_config = ansible_module.params['destination_config']

        result = dict(
            changed=False
        )

        if ansible_module.check_mode:
            ansible_module.exit_json(**result)

        # Get session serialized information
        session_info = session.get_session()
        # Create pyaoscx.session object
        s = Pyaoscx_Session.from_session(
            session_info['s'], session_info['url'])

        # Create a Device Object
        device = Device(s)

        # Create a Configuration Object
        config = device.configuration()
        success = config.create_checkpoint(source_config, destination_config)

        # Changed
        result['changed'] = success

        # Exit
        ansible_module.exit_json(**result)

    # Use Older version
    else:
        aruba_ansible_module = ArubaAnsibleModule(module_args=module_args)

        source_config = aruba_ansible_module.module.params['source_config']
        destination_config = \
            aruba_ansible_module.module.params['destination_config']

        url = '/rest/v1/fullconfigs/{dest}?from=/rest/v1/fullconfigs/{src}'.format(
            dest=destination_config, src=source_config)
        put(aruba_ansible_module.module, url)
        result = dict(changed=aruba_ansible_module.changed,
                      warnings=aruba_ansible_module.warnings)
        result["changed"] = True
        aruba_ansible_module.module.exit_json(**result)
Exemplo n.º 3
0
def main():
    module_args = dict(config_name=dict(type='str', default='running-config'),
                       config_json=dict(type='str', default=None),
                       config_file=dict(type='str', default=None),
                       remote_config_file_tftp_path=dict(type='str',
                                                         default=None),
                       vrf=dict(type='str'))

    # Use PYAOSCX SDK
    if USE_PYAOSCX_SDK:
        from ansible.module_utils.basic import AnsibleModule

        # ArubaModule
        ansible_module = AnsibleModule(argument_spec=module_args,
                                       supports_check_mode=True)

        # Session
        session = Session(ansible_module)

        # Set Variables
        tftp_path = ansible_module.params['remote_config_file_tftp_path']
        vrf = ansible_module.params['vrf']
        config_name = ansible_module.params['config_name']
        config_json = ansible_module.params['config_json']
        config_file = ansible_module.params['config_file']

        result = dict(changed=False)

        if ansible_module.check_mode:
            ansible_module.exit_json(**result)

        # Get session's serialized information
        session_info = session.get_session()
        # Create pyaoscx session object
        s = Pyaoscx_Session.from_session(session_info['s'],
                                         session_info['url'])

        # Create a Device Object
        device = Device(s)

        # Create an instance of the Configuration class from Device
        config = device.configuration()  # Contains action methods

        # Upload configuration file/json to switch
        success = config.upload_switch_config(config_name=config_name,
                                              config_file=config_file,
                                              config_json=config_json,
                                              vrf=vrf,
                                              remote_file_tftp_path=tftp_path)

        # Changed
        result['changed'] = success

        # Exit
        ansible_module.exit_json(**result)

    # Use Older Version
    else:

        aruba_ansible_module = ArubaAnsibleModule(module_args=module_args,
                                                  store_config=False)

        tftp_path = aruba_ansible_module.module.params[
            "remote_config_file_tftp_path"]
        vrf = aruba_ansible_module.module.params['vrf']
        config_name = aruba_ansible_module.module.params['config_name']
        config_json = aruba_ansible_module.module.params['config_json']
        config_file = aruba_ansible_module.module.params['config_file']

        if tftp_path is not None:
            if vrf is None:
                aruba_ansible_module.module.fail_json(
                    msg=("VRF needs to be provided in order to TFTP"
                         " the configuration onto the switch"))
            tftp_path_replace = tftp_path.replace("/", "%2F")
            tftp_path_encoded = tftp_path_replace.replace(":", "%3A")
            if config_name != 'running-config' and config_name != 'startup-config':
                aruba_ansible_module.module.fail_json(
                    msg=("Only running-config or startup-config "
                         "can be uploaded using TFTP"))
            aruba_ansible_module.tftp_switch_config_from_remote_location(
                tftp_path_encoded, config_name, vrf)
        else:
            if config_json:
                with open(config_json) as json_file:
                    config_json = json.load(json_file)

            if config_file:
                with open(config_file) as json_file:
                    config_json = json.load(json_file)

            aruba_ansible_module.upload_switch_config(config_json, config_name)

        result = dict(changed=aruba_ansible_module.changed,
                      warnings=aruba_ansible_module.warnings)
        result["changed"] = True
        aruba_ansible_module.module.exit_json(**result)