Beispiel #1
0
def main():
    ansible_module = AnsibleModule(
        argument_spec=get_argument_spec(),
        supports_check_mode=True,
    )

    result = dict(changed=False)

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

    session = get_pyaoscx_session(ansible_module)

    # Get Ansible module's parameters
    name = ansible_module.params["name"]
    vsx_sync = ansible_module.params["vsx_sync"]
    state = ansible_module.params["state"]

    kwargs = {}
    if vsx_sync:
        kwargs["vsx_sync"] = vsx_sync

    q_profile = QueueProfile(session, name, **kwargs)
    try:
        q_profile.get()
        exists = True
    except Exception as exc:
        exists = False

    if state == "delete":
        device = Device(session)
        if not device.materialized:
            device.get()
        if device.q_profile_default == name:
            msg = ("Skipping, cannot delete Queue Profile, while it is set "
                   "as global.")
            result["msg"] = msg
            result["skipped"] = True
        else:
            if exists:
                q_profile.delete()
            result["changed"] = exists
    else:
        if not exists:
            result["changed"] = True
        for k, v in kwargs.items():
            if hasattr(q_profile, k):
                result["changed"] |= getattr(q_profile, k) != v
            else:
                result["changed"] = True
            setattr(q_profile, k, v)
        if not exists or result["changed"]:
            q_profile.apply()

    # Exit
    ansible_module.exit_json(**result)
Beispiel #2
0
    def device(self):
        """
        Create a Device class, to obtain common device configuration,
        capacities, capabilities, among other information related.
        :return: Device object
        """

        switch = Device(self.session)
        # Get Partial configuration attributes
        switch.get()
        return switch
Beispiel #3
0
def main():
    ansible_module = AnsibleModule(
        argument_spec=get_argument_spec(),
        supports_check_mode=True,
    )

    result = dict(changed=False)

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

    session = get_pyaoscx_session(ansible_module)

    # Get playbook's arguments
    name = ansible_module.params["name"]
    state = ansible_module.params["state"]
    vsx_sync = ansible_module.params["vsx_sync"]

    device = Device(session)

    schedule_profile_kw = {}
    if vsx_sync:
        schedule_profile_kw["vsx_sync"] = vsx_sync

    if state == "delete":
        if not device.materialized:
            device.get()
        if device.qos_default == name:
            msg = ("Skipping, cannot delete Schedule Profile, while it is set "
                   "as global.")
            result["msg"] = msg
            result["skipped"] = True
        else:
            sched_profile = device.qos(name)
            result["changed"] = sched_profile.was_modified()
            sched_profile.delete()
            # only report deletion if object existed before call to ansible
            result["changed"] = not result["changed"]
    else:
        sched_profile = device.qos(name, **schedule_profile_kw)
        result["changed"] = sched_profile.was_modified()

    # Exit
    ansible_module.exit_json(**result)
def main():
    # Create the Ansible Module
    ansible_module = AnsibleModule(
        argument_spec=get_argument_spec(),
        supports_check_mode=True,
        required_together=[["duplex", "speeds"]],
        mutually_exclusive=[
            ("qos", "no_qos"),
            ("queue_profile", "use_global_queue_profile"),
        ],
    )

    result = dict(changed=False)

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

    # Get playbook's arguments
    name = ansible_module.params["name"]
    enabled = ansible_module.params["enabled"]
    description = ansible_module.params["description"]
    duplex = ansible_module.params["duplex"]
    speeds = ansible_module.params["speeds"]
    vsx_sync = ansible_module.params["vsx_sync"]
    state = ansible_module.params["state"]
    qos = ansible_module.params["qos"]
    no_qos = ansible_module.params["no_qos"]
    queue_profile = ansible_module.params["queue_profile"]
    use_global_queue_profile = ansible_module.params[
        "use_global_queue_profile"]
    qos_trust_mode = ansible_module.params["qos_trust_mode"]
    qos_rate = ansible_module.params["qos_rate"]

    session = get_pyaoscx_session(ansible_module)
    device = Device(session)

    interface = device.interface(name)
    modified = interface.modified

    if state == "delete":
        interface.delete()
        # report only if created before this run
        result["changed"] = not modified
        ansible_module.exit_json(**result)

    if description:
        interface.description = description
    if enabled is not None:
        interface.admin_state = "up" if enabled else "down"
    if vsx_sync:
        if not device.materialized:
            device.get()
        if not device.vsx_capable():
            ansible_module.module.fail_json(msg="Device doesn't support VSX")
        clean_vsx_features = [
            vsx_sync_features_mapping(feature) for feature in vsx_sync
        ]
        interface.vsx_sync = clean_vsx_features

    modified |= interface.apply()
    if duplex and speeds:
        modified |= interface.speed_duplex_configure(speeds=speeds,
                                                     duplex=duplex)
    if qos:
        modified |= interface.update_interface_qos(qos)
    if no_qos:
        modified |= interface.update_interface_qos(None)
    if queue_profile:
        modified |= interface.update_interface_queue_profile(queue_profile)
    if use_global_queue_profile:
        modified |= interface.update_interface_queue_profile(None)
    if qos_trust_mode:
        modified |= interface.update_interface_qos_trust_mode(qos_trust_mode)
    if qos_rate:
        modified |= interface.update_interface_qos_rate(qos_rate)

    result["changed"] = modified
    ansible_module.exit_json(**result)
Beispiel #5
0
def main():
    """
    Main entry point for module execution
    :returns: ansible_facts
    """
    argument_spec = {
        'gather_subset':
        dict(default=[
            'software_info', 'software_images', 'host_name', 'platform_name',
            'management_interface', 'software_version', 'fans',
            'power_supplies', 'product_info', 'physical_interfaces',
            'resource_utilization', 'domain_name'
        ],
             type='list',
             choices=[
                 'software_info', 'software_images', 'host_name',
                 'platform_name', 'management_interface', 'software_version',
                 'config', 'fans', 'power_supplies', 'product_info',
                 'physical_interfaces', 'resource_utilization', 'domain_name'
             ]),
        'gather_network_resources':
        dict(type='list', choices=['interfaces', 'vlans', 'vrfs'])
    }

    # Version Management
    try:

        from ansible.module_utils.aoscx_pyaoscx import Session
        from pyaoscx.session import Session as Pyaoscx_Session
        from pyaoscx.interface import Interface
        from pyaoscx.vlan import Vlan
        from pyaoscx.device import Device
        from pyaoscx.vrf import Vrf

        USE_PYAOSCX_SDK = True

    except ImportError:
        USE_PYAOSCX_SDK = False

    # Use the PYAOSCX SDK
    if USE_PYAOSCX_SDK:

        argument_spec.update(aoscx_http_argument_spec)

        ansible_module = AnsibleModule(argument_spec=argument_spec,
                                       supports_check_mode=True)

        # Get session
        session = Session(ansible_module)

        # Session info
        session_info = session.get_session()

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

        warnings = []
        if ansible_module.params["gather_subset"] == "!config":
            warnings.append(
                'default value for `gather_subset` will be changed '
                'to `min` from `!config` v2.11 onwards')

        # Declare the Ansible facts
        ansible_facts = {}

        # Retrieve variables from module parameters
        network_resource_list = ansible_module.params[
            'gather_network_resources']
        subset_list = ansible_module.params['gather_subset']

        # Retrieve ansible_network_resources
        ansible_network_resources = {}
        if network_resource_list is not None:
            for resource in network_resource_list:
                if resource == 'interfaces':
                    ansible_network_resources.update(
                        {'interfaces': Interface.get_facts(s)})
                elif resource == 'vlans':
                    ansible_network_resources.update(
                        {'vlans': Vlan.get_facts(s)})
                elif resource == 'vrfs':
                    ansible_network_resources.update(
                        {'vrfs': Vrf.get_facts(s)})

        ansible_facts.update(
            {'ansible_network_resources': ansible_network_resources})

        # Retrieve ansible_net_gather_network_resources
        ansible_facts.update(
            {'ansible_net_gather_network_resources': network_resource_list})

        # Retrieve ansible_net_gather_subset
        ansible_facts.update({'ansible_net_gather_subset': subset_list})

        # Retrieve device facts
        switch = Device(s)
        switch.get()
        switch.get_subsystems()  # subsystem

        # Set the subsystem attributes allowed to retrieve as facts
        allowed_subsystem_attributes = [
            'product_info', 'power_supplies', 'interfaces', 'fans',
            'resource_utilization'
        ]

        # Set the default subsets that are always retreived as facts
        default_subset_list = ['management_interface', 'software_version']

        # Extend subset_list with default subsets
        subset_list.extend(default_subset_list)

        # Delete duplicates
        subset_list = list(dict.fromkeys(subset_list))

        # Iterate through given subset arguments in the gather_subset parameter
        # in argument_spec
        for subset in subset_list:

            # Argument translation for management_interface and
            # physical_interfaces
            if subset == 'management_interface':
                subset = 'mgmt_intf_status'
            elif subset == 'physical_interfaces':
                subset = 'interfaces'
            elif subset == 'host_name':
                subset = 'hostname'

            str_subset = 'ansible_net_' + subset

            # Check if current subset is inside the Device object
            if hasattr(switch, subset):

                # Get attribute value and add it to Ansible facts dictionary
                ansible_facts[str_subset] = getattr(switch, subset)

            # Check if current subset is inside the allowed subsystem
            # attributes
            elif subset in allowed_subsystem_attributes:
                ansible_facts.update({str_subset: {}})

                # Iterate through Device subsystems
                for subsystem, value in switch.subsystems.items():

                    # Get attribute value and update the Ansible facts
                    # dictionary
                    ansible_facts[str_subset].update(
                        {subsystem: switch.subsystems[subsystem][subset]})

        ansible_module.exit_json(ansible_facts=ansible_facts,
                                 warnings=warnings)

    # USE OLD VERSION
    else:
        argument_spec.update(aoscx_http_argument_spec)

        module = AnsibleModule(argument_spec=argument_spec,
                               supports_check_mode=True)

        module._connection = get_connection(module)  # noqa

        warnings = []
        if module.params["gather_subset"] == "!config":
            warnings.append(
                'default value for `gather_subset` will be changed '
                'to `min` from `!config` v2.11 onwards')

        result = Facts(module).get_facts()

        ansible_facts, additional_warnings = result
        warnings.extend(additional_warnings)

        module.exit_json(ansible_facts=ansible_facts, warnings=warnings)