Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix eos_l2_interface insufficient commands #50754

Merged
merged 1 commit into from Jan 10, 2019

Conversation

tetsuyasodo
Copy link
Contributor

SUMMARY

Fixes #50639

ISSUE TYPE
  • Bugfix Pull Request
COMPONENT NAME

modules/network/eos/eos_l2_interface.py

ADDITIONAL INFORMATION

This is an additional fix on PR #50644

@ansibot
Copy link
Contributor

ansibot commented Jan 10, 2019

@ansibot
Copy link
Contributor

ansibot commented Jan 10, 2019

@tetsuyasodo, just so you are aware we have a dedicated Working Group for network.
You can find other people interested in this in #ansible-network on Freenode IRC
For more information about communities, meetings and agendas see https://github.com/ansible/community

click here for bot help

@ansibot ansibot added affects_2.8 This issue/PR affects Ansible v2.8 bug This issue/PR relates to a bug. core_review In order to be merged, this PR must follow the core review workflow. module This issue/PR relates to a module. needs_triage Needs a first human triage before being processed. networking Network category new_contributor This PR is the first contribution by a new community member. small_patch support:network This issue/PR relates to code supported by the Ansible Network Team. labels Jan 10, 2019
@trishnaguha trishnaguha added this to Needs Triage in Networking via automation Jan 10, 2019
@trishnaguha trishnaguha merged commit 1d4dbd7 into ansible:devel Jan 10, 2019
Networking automation moved this from Needs Triage to Done Jan 10, 2019
@trishnaguha trishnaguha removed the needs_triage Needs a first human triage before being processed. label Jan 10, 2019
@trishnaguha
Copy link
Member

Thanks for the PR @tetsuyasodo

@trishnaguha trishnaguha self-assigned this Jan 10, 2019
kbreit pushed a commit to kbreit/ansible that referenced this pull request Jan 11, 2019
knumskull pushed a commit to knumskull/ansible that referenced this pull request Jan 21, 2019
@thukkel
Copy link

thukkel commented Mar 26, 2019

This patch seems to be incomplete and there is still issues when ports change modes. After line 195 there must be a commands.append('switchport mode trunk') or trunk mode will not be set if there is no mode already configured.

Also, if the mode is 'access' when parsing the current config, we need to do additional parsing to set the mode to access since we cannot rely on access_vlan alone -- since switchport access vlan can be manually set on the port outside ansible and will break consistency.

Please consider the following changes:

def map_obj_to_commands(updates, module):
    commands = list()
    want, have = updates

    for w in want:
        name = w['name']
        state = w['state']
        mode = w['mode']
        access_vlan = w['access_vlan']
        native_vlan = w['native_vlan']
        trunk_allowed_vlans = w['trunk_allowed_vlans']

        interface = 'interface ' + name
        commands.append(interface)

        obj_in_have = search_obj_in_list(name, have)
        if not obj_in_have:
            module.fail_json(msg='invalid interface {0}'.format(name))

        if state == 'absent':
            if obj_in_have['mode'] == 'access':
                commands.append('no switchport access vlan {0}'.format(obj_in_have['access_vlan']))

            if obj_in_have['mode'] == 'trunk':
                commands.append('no switchport mode trunk')

            if obj_in_have['native_vlan']:
                commands.append('no switchport trunk native vlan {0}'.format(obj_in_have['native_vlan']))

            if obj_in_have['trunk_allowed_vlans']:
                commands.append('no switchport trunk allowed vlan {0}'.format(obj_in_have['trunk_allowed_vlans']))

            if obj_in_have['state'] == 'present':
                commands.append('no switchport')
        else:
            if obj_in_have['state'] == 'absent':
                commands.append('switchport')
                commands.append('switchport mode {0}'.format(mode))

                if access_vlan:
                    commands.append('switchport access vlan {0}'.format(access_vlan))

                if native_vlan:
                    commands.append('switchport trunk native vlan {0}'.format(native_vlan))

                if trunk_allowed_vlans:
                    commands.append('switchport trunk allowed vlan {0}'.format(trunk_allowed_vlans))
            else:
                if mode != obj_in_have['mode']:
                    if obj_in_have['mode'] == 'access':
                        commands.append('no switchport access vlan {0}'.format(obj_in_have['access_vlan']))
                        if native_vlan:
                            commands.append('switchport trunk native vlan {0}'.format(native_vlan))
                        if trunk_allowed_vlans:
                            commands.append('switchport trunk allowed vlan {0}'.format(trunk_allowed_vlans))
                        commands.append('switchport mode trunk')
                    else:
                        if obj_in_have['native_vlan']:
                            commands.append('no switchport trunk native vlan {0}'.format(obj_in_have['native_vlan']))
                        if obj_in_have['trunk_allowed_vlans']:
                            commands.append('no switchport trunk allowed vlan {0}'.format(obj_in_have['trunk_allowed_vlans']))
                        commands.append('switchport access vlan {0}'.format(access_vlan))
                        commands.append('no switchport mode trunk')
                else:
                    if mode == 'access':
                        if access_vlan != obj_in_have['access_vlan']:
                            commands.append('switchport access vlan {0}'.format(access_vlan))
                        commands.append('no switchport mode trunk')
                    else:
                        if native_vlan != obj_in_have['native_vlan'] and native_vlan:
                            commands.append('switchport trunk native vlan {0}'.format(native_vlan))
                        if trunk_allowed_vlans != obj_in_have['trunk_allowed_vlans'] and trunk_allowed_vlans:
                            commands.append('switchport trunk allowed vlan {0}'.format(trunk_allowed_vlans))
                        commands.append('switchport mode trunk')

        if commands[-1] == interface:
            commands.pop(-1)

    return commands


def map_config_to_obj(module):
    config = get_config(module, flags=['| section interface'])
    configobj = NetworkConfig(indent=3, contents=config)

    match = re.findall(r'^interface (\S+)', config, re.M)
    if not match:
        return list()

    instances = list()

    for item in set(match):
        command = {'command': 'show interfaces {0} switchport | include Switchport'.format(item),
                   'output': 'text'}
        switchport_cfg = run_commands(module, command)[0].split(':')[1].strip()
        if switchport_cfg == 'Enabled':
            state = 'present'
        else:
            state = 'absent'

        obj = {
            'name': item.lower(),
            'state': state,
        }

        obj['access_vlan'] = parse_config_argument(configobj, item, 'switchport access vlan')
        obj['native_vlan'] = parse_config_argument(configobj, item, 'switchport trunk native vlan')
        obj['trunk_allowed_vlans'] = parse_config_argument(configobj, item, 'switchport trunk allowed vlan')
        switchport_mode = parse_config_argument(configobj, item, 'switchport mode')
        if not switchport_mode:
            obj['mode'] = 'access'
        elif 'trunk' in switchport_mode:
            obj['mode'] = 'trunk'
        elif switchport_mode in ['dot1q-tunnel', 'tap', 'tap-tool', 'tool']:
            obj['mode'] = 'other'
        else:
            obj['mode'] = 'access'
        instances.append(obj)

    return instances

trishnaguha pushed a commit to trishnaguha/ansible that referenced this pull request Apr 1, 2019
abadger pushed a commit that referenced this pull request Apr 4, 2019
@ansible ansible locked and limited conversation to collaborators Jul 22, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affects_2.8 This issue/PR affects Ansible v2.8 bug This issue/PR relates to a bug. core_review In order to be merged, this PR must follow the core review workflow. module This issue/PR relates to a module. networking Network category new_contributor This PR is the first contribution by a new community member. small_patch support:network This issue/PR relates to code supported by the Ansible Network Team.
Projects
No open projects
Networking
  
Done
Development

Successfully merging this pull request may close these issues.

eos_l2_interface unable to change port mode between access and trunk
4 participants