コード例 #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))

    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']
    sort_json = aruba_ansible_module.module.params['sort_json']

    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)
        if sort_json:
            config_json = comp_sort(config_json)
        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)
コード例 #2
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_collections.arubanetworks.aoscx.plugins.module_utils.aoscx_pyaoscx import Session
        from pyaoscx.session import Session as Pyaoscx_Session
        from pyaoscx.pyaoscx_factory import PyaoscxFactory

        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 Pyaoscx Factory Object
        pyaoscx_factory = PyaoscxFactory(s)

        # Create a Configuration Object
        config = pyaoscx_factory.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']
        sort_json = aruba_ansible_module.module.params['sort_json']

        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)
            if sort_json:
                config_json = comp_sort(config_json)
            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)