Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- - name: Device interface tests
- hosts: TTTSW
- gather_facts: no
- #check_mode: yes
- #debugger: always
- strategy: debug
- vars:
- port_channel_id: 121
- vpc_number: 121
- interface: Ethernet1/21
- vlans:
- native_vlan:
- port_profile_name: test-ansible2
- interface_description: kuku
- tasks:
- - name: "Show port-profile brief to see if needed port-profile exists"
- nxos_command:
- commands:
- - "show port-profile brief | include Port-channel"
- register: port_profile_brief
- when: "port_profile_name != None and vlans == None"
- failed_when: "port_profile_name not in port_profile_brief.stdout_lines[0] | map('trim') | map('split') | map('first')"
- ignore_errors: '{{ ansible_check_mode }}'
- - name: "Get physical interface running-configuration"
- ansible.utils.cli_parse:
- command: 'show running-config interface {{ interface }}'
- parser:
- name: ansible.netcommon.pyats
- set_fact: interface_running_config
- - name: "Show port-channel database to see if port-channel{{port_channel_id}} exists before creation"
- ansible.utils.cli_parse:
- command: 'show port-channel summary'
- parser:
- name: ansible.netcommon.pyats
- set_fact: port_channel_database_before
- - name: "Configure port-channel only if physical interface {{interface}} doesnt have any port-channel configuration and port-channel{{port_channel_id}} is not already present"
- nxos_lag_interfaces:
- config:
- - name: "port-channel{{port_channel_id}}"
- members:
- - member: "{{ interface }}"
- mode: active
- force: true
- state: merged
- when:
- - "'port_channel' not in interface_running_config['interface'][interface] and 'Port-channel' ~ port_channel_id not in port_channel_database_before['interfaces'].keys()"
- - name: "Show port-channel database to double check if port-channel{{port_channel_id}} exists after creation"
- ansible.utils.cli_parse:
- command: 'show port-channel summary'
- parser:
- name: ansible.netcommon.pyats
- set_fact: port_channel_database
- - name: "Get port-channel running-configuration if port-channel{{port_channel_id}} exists"
- nxos_command:
- commands:
- - "show running-config interface port-channel{{port_channel_id}}"
- register: portchannel_running_config
- when: "'Port-channel' ~ port_channel_id in port_channel_database['interfaces'].keys()"
- - name: "Show VPC brief if the VPC number {{vpc_number}} is not configured under interface"
- ansible.utils.cli_parse:
- command: 'show vpc'
- parser:
- name: ansible.netcommon.pyats
- set_fact: vpc_brief
- when:
- - "vpc_number != None"
- - "portchannel_running_config.stdout_lines[0] is defined and 'vpc ' ~ vpc_number not in portchannel_running_config.stdout_lines[0] | map('trim') | list"
- failed_when: portchannel_running_config.stdout_lines[0] is search ('\svpc\s\d{1,4}') and 'vpc ' ~ vpc_number not in portchannel_running_config.stdout_lines[0] | map('trim') | list
- ignore_errors: '{{ ansible_check_mode }}'
- - name: "Configure VPC number {{vpc_number}} if it doesnt exist elsewhere"
- nxos_config:
- lines:
- - "vpc {{vpc_number}}"
- parents: "interface port-channel{{port_channel_id}}"
- when:
- - "vpc_number != None"
- - "vpc_brief is defined and portchannel_running_config.stdout_lines[0] is defined"
- failed_when: "vpc_number|string in vpc_brief['vpc'].keys() and 'vpc ' ~ vpc_number not in portchannel_running_config.stdout_lines[0] | map('trim') | list"
- ignore_errors: '{{ ansible_check_mode }}'
- - name: "Remove wrong port-profile ({{ portchannel_running_config.stdout[0] | regex_search('inherit port-profile.*') }}) and add the specified one ({{ port_profile_name }})"
- nxos_config:
- lines:
- - shutdown
- - switchport mode trunk
- - "no {{ portchannel_running_config.stdout[0] | regex_search('inherit port-profile.*') }}"
- - no switchport trunk allowed vlan
- - no switchport trunk native vlan
- - inherit port-profile {{port_profile_name}}
- - no lacp suspend-individual
- - no shutdown
- parents: "interface port-channel{{port_channel_id}}"
- register: change_profile
- when:
- - "portchannel_running_config.stdout_lines[0] is defined and port_profile_name != None and vlans == None"
- - "portchannel_running_config.stdout_lines[0] is search ('inherit port-profile') and 'inherit port-profile ' ~ port_profile_name not in portchannel_running_config.stdout_lines[0] | map('trim') | list"
- - name: "Add port-profile ({{port_profile_name}}) to port-channel{{port_channel_id}}"
- nxos_config:
- lines:
- - shutdown
- - switchport mode trunk
- - no switchport trunk allowed vlan
- - no switchport trunk native vlan
- - inherit port-profile {{port_profile_name}}
- - no lacp suspend-individual
- - no shutdown
- parents: "interface port-channel{{port_channel_id}}"
- register: add_profile
- when:
- - "portchannel_running_config.stdout_lines[0] is defined and port_profile_name != None and vlans == None"
- - "portchannel_running_config.stdout_lines[0] is not search ('inherit port-profile') and change_profile.changed == false"
- - name: "Remove port-profile ({{ portchannel_running_config.stdout[0] | regex_search('inherit port-profile.*') }}) to add separate vlans"
- nxos_config:
- lines:
- - shutdown
- - switchport mode trunk
- - "no {{ portchannel_running_config.stdout[0] | regex_search('inherit port-profile.*') }}"
- - no lacp suspend-individual
- - no shutdown
- parents: "interface port-channel{{port_channel_id}}"
- register: remove_profile
- when:
- - "portchannel_running_config.stdout_lines[0] is defined and port_profile_name == None and (vlans != None or native_vlan != None)"
- - "portchannel_running_config.stdout_lines[0] is search ('inherit port-profile')"
- - name: "Add required native vlan ({{ native_vlan }}) "
- nxos_config:
- lines:
- - switchport trunk native vlan {{native_vlan}}
- parents: "interface port-channel{{port_channel_id}}"
- when:
- - "port_profile_name == None and native_vlan != None"
- - name: "Add required trunk vlans ({{ vlans }}) "
- nxos_config:
- lines:
- - switchport trunk allowed vlan {{ vlans }}
- parents: "interface port-channel{{port_channel_id}}"
- when:
- - "port_profile_name == None and vlans != None"
- - name: "Add switchport mode trunk if not defined "
- nxos_config:
- lines:
- - switchport mode trunk
- parents: "interface port-channel{{port_channel_id}}"
- - name: "Check and remove switchport trunk allowed vlan if port-profile is present"
- nxos_config:
- lines:
- - no switchport trunk allowed vlan
- parents: "interface port-channel{{port_channel_id}}"
- when:
- - "portchannel_running_config.stdout_lines[0] is defined and port_profile_name != None and vlans == None"
- - "portchannel_running_config.stdout_lines[0] is search ('inherit port-profile') and portchannel_running_config.stdout_lines[0] is search ('switchport trunk allowed vlan')"
- - name: "Check and remove switchport trunk native vlan if port-profile is present"
- nxos_config:
- lines:
- - no switchport trunk native vlan
- parents: "interface port-channel{{port_channel_id}}"
- when:
- - "portchannel_running_config.stdout_lines[0] is defined and port_profile_name != None and vlans == None"
- - "portchannel_running_config.stdout_lines[0] is search ('inherit port-profile') and portchannel_running_config.stdout_lines[0] is search ('switchport trunk native vlan')"
- - name: "Add 'no lacp suspend-individual' if port-channel{{port_channel_id}} is present and 'no lacp suspend-individual' is missing"
- nxos_config:
- lines:
- - shutdown
- - no lacp suspend-individual
- - no shutdown
- parents: "interface port-channel{{port_channel_id}}"
- when: portchannel_running_config.stdout_lines[0] is defined and portchannel_running_config.stdout_lines[0] is not search ('no lacp suspend-individual') and add_profile.changed == false and change_profile.changed == false and remove_profile.changed == false
- - name: "Add port-channel{{port_channel_id}} description ({{interface_description}}) if there're no description or description is wrong ({{ portchannel_running_config.stdout[0] | regex_search('description.*') }})"
- nxos_config:
- lines:
- - description {{interface_description}}
- parents: "interface port-channel{{port_channel_id}}"
- - name: "Add {{interface}} description ({{interface_description}}) if there're no description or description is wrong ({{ interface_running_config['interface'][interface]['description'] | default('no description defined') }})"
- nxos_config:
- lines:
- - description {{interface_description}}
- parents: "interface {{ interface }}"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement