Advertisement
Guest User

Untitled

a guest
Apr 6th, 2023
309
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.50 KB | None | 0 0
  1. - name: Device interface tests
  2. hosts: TTTSW
  3. gather_facts: no
  4. #check_mode: yes
  5. #debugger: always
  6. strategy: debug
  7. vars:
  8. port_channel_id: 121
  9. vpc_number: 121
  10. interface: Ethernet1/21
  11. vlans:
  12. native_vlan:
  13. port_profile_name: test-ansible2
  14. interface_description: kuku
  15. tasks:
  16. - name: "Show port-profile brief to see if needed port-profile exists"
  17. nxos_command:
  18. commands:
  19. - "show port-profile brief | include Port-channel"
  20. register: port_profile_brief
  21. when: "port_profile_name != None and vlans == None"
  22. failed_when: "port_profile_name not in port_profile_brief.stdout_lines[0] | map('trim') | map('split') | map('first')"
  23. ignore_errors: '{{ ansible_check_mode }}'
  24.  
  25. - name: "Get physical interface running-configuration"
  26. ansible.utils.cli_parse:
  27. command: 'show running-config interface {{ interface }}'
  28. parser:
  29. name: ansible.netcommon.pyats
  30. set_fact: interface_running_config
  31.  
  32. - name: "Show port-channel database to see if port-channel{{port_channel_id}} exists before creation"
  33. ansible.utils.cli_parse:
  34. command: 'show port-channel summary'
  35. parser:
  36. name: ansible.netcommon.pyats
  37. set_fact: port_channel_database_before
  38.  
  39. - 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"
  40. nxos_lag_interfaces:
  41. config:
  42. - name: "port-channel{{port_channel_id}}"
  43. members:
  44. - member: "{{ interface }}"
  45. mode: active
  46. force: true
  47. state: merged
  48. when:
  49. - "'port_channel' not in interface_running_config['interface'][interface] and 'Port-channel' ~ port_channel_id not in port_channel_database_before['interfaces'].keys()"
  50.  
  51. - name: "Show port-channel database to double check if port-channel{{port_channel_id}} exists after creation"
  52. ansible.utils.cli_parse:
  53. command: 'show port-channel summary'
  54. parser:
  55. name: ansible.netcommon.pyats
  56. set_fact: port_channel_database
  57.  
  58. - name: "Get port-channel running-configuration if port-channel{{port_channel_id}} exists"
  59. nxos_command:
  60. commands:
  61. - "show running-config interface port-channel{{port_channel_id}}"
  62. register: portchannel_running_config
  63. when: "'Port-channel' ~ port_channel_id in port_channel_database['interfaces'].keys()"
  64.  
  65. - name: "Show VPC brief if the VPC number {{vpc_number}} is not configured under interface"
  66. ansible.utils.cli_parse:
  67. command: 'show vpc'
  68. parser:
  69. name: ansible.netcommon.pyats
  70. set_fact: vpc_brief
  71. when:
  72. - "vpc_number != None"
  73. - "portchannel_running_config.stdout_lines[0] is defined and 'vpc ' ~ vpc_number not in portchannel_running_config.stdout_lines[0] | map('trim') | list"
  74. 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
  75. ignore_errors: '{{ ansible_check_mode }}'
  76.  
  77. - name: "Configure VPC number {{vpc_number}} if it doesnt exist elsewhere"
  78. nxos_config:
  79. lines:
  80. - "vpc {{vpc_number}}"
  81. parents: "interface port-channel{{port_channel_id}}"
  82. when:
  83. - "vpc_number != None"
  84. - "vpc_brief is defined and portchannel_running_config.stdout_lines[0] is defined"
  85. 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"
  86. ignore_errors: '{{ ansible_check_mode }}'
  87.  
  88. - name: "Remove wrong port-profile ({{ portchannel_running_config.stdout[0] | regex_search('inherit port-profile.*') }}) and add the specified one ({{ port_profile_name }})"
  89. nxos_config:
  90. lines:
  91. - shutdown
  92. - switchport mode trunk
  93. - "no {{ portchannel_running_config.stdout[0] | regex_search('inherit port-profile.*') }}"
  94. - no switchport trunk allowed vlan
  95. - no switchport trunk native vlan
  96. - inherit port-profile {{port_profile_name}}
  97. - no lacp suspend-individual
  98. - no shutdown
  99. parents: "interface port-channel{{port_channel_id}}"
  100. register: change_profile
  101. when:
  102. - "portchannel_running_config.stdout_lines[0] is defined and port_profile_name != None and vlans == None"
  103. - "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"
  104.  
  105. - name: "Add port-profile ({{port_profile_name}}) to port-channel{{port_channel_id}}"
  106. nxos_config:
  107. lines:
  108. - shutdown
  109. - switchport mode trunk
  110. - no switchport trunk allowed vlan
  111. - no switchport trunk native vlan
  112. - inherit port-profile {{port_profile_name}}
  113. - no lacp suspend-individual
  114. - no shutdown
  115. parents: "interface port-channel{{port_channel_id}}"
  116. register: add_profile
  117. when:
  118. - "portchannel_running_config.stdout_lines[0] is defined and port_profile_name != None and vlans == None"
  119. - "portchannel_running_config.stdout_lines[0] is not search ('inherit port-profile') and change_profile.changed == false"
  120.  
  121. - name: "Remove port-profile ({{ portchannel_running_config.stdout[0] | regex_search('inherit port-profile.*') }}) to add separate vlans"
  122. nxos_config:
  123. lines:
  124. - shutdown
  125. - switchport mode trunk
  126. - "no {{ portchannel_running_config.stdout[0] | regex_search('inherit port-profile.*') }}"
  127. - no lacp suspend-individual
  128. - no shutdown
  129. parents: "interface port-channel{{port_channel_id}}"
  130. register: remove_profile
  131. when:
  132. - "portchannel_running_config.stdout_lines[0] is defined and port_profile_name == None and (vlans != None or native_vlan != None)"
  133. - "portchannel_running_config.stdout_lines[0] is search ('inherit port-profile')"
  134.  
  135. - name: "Add required native vlan ({{ native_vlan }}) "
  136. nxos_config:
  137. lines:
  138. - switchport trunk native vlan {{native_vlan}}
  139. parents: "interface port-channel{{port_channel_id}}"
  140. when:
  141. - "port_profile_name == None and native_vlan != None"
  142.  
  143. - name: "Add required trunk vlans ({{ vlans }}) "
  144. nxos_config:
  145. lines:
  146. - switchport trunk allowed vlan {{ vlans }}
  147. parents: "interface port-channel{{port_channel_id}}"
  148. when:
  149. - "port_profile_name == None and vlans != None"
  150.  
  151. - name: "Add switchport mode trunk if not defined "
  152. nxos_config:
  153. lines:
  154. - switchport mode trunk
  155. parents: "interface port-channel{{port_channel_id}}"
  156.  
  157. - name: "Check and remove switchport trunk allowed vlan if port-profile is present"
  158. nxos_config:
  159. lines:
  160. - no switchport trunk allowed vlan
  161. parents: "interface port-channel{{port_channel_id}}"
  162. when:
  163. - "portchannel_running_config.stdout_lines[0] is defined and port_profile_name != None and vlans == None"
  164. - "portchannel_running_config.stdout_lines[0] is search ('inherit port-profile') and portchannel_running_config.stdout_lines[0] is search ('switchport trunk allowed vlan')"
  165.  
  166. - name: "Check and remove switchport trunk native vlan if port-profile is present"
  167. nxos_config:
  168. lines:
  169. - no switchport trunk native vlan
  170. parents: "interface port-channel{{port_channel_id}}"
  171. when:
  172. - "portchannel_running_config.stdout_lines[0] is defined and port_profile_name != None and vlans == None"
  173. - "portchannel_running_config.stdout_lines[0] is search ('inherit port-profile') and portchannel_running_config.stdout_lines[0] is search ('switchport trunk native vlan')"
  174.  
  175. - name: "Add 'no lacp suspend-individual' if port-channel{{port_channel_id}} is present and 'no lacp suspend-individual' is missing"
  176. nxos_config:
  177. lines:
  178. - shutdown
  179. - no lacp suspend-individual
  180. - no shutdown
  181. parents: "interface port-channel{{port_channel_id}}"
  182. 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
  183.  
  184. - 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.*') }})"
  185. nxos_config:
  186. lines:
  187. - description {{interface_description}}
  188. parents: "interface port-channel{{port_channel_id}}"
  189.  
  190. - 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') }})"
  191. nxos_config:
  192. lines:
  193. - description {{interface_description}}
  194. parents: "interface {{ interface }}"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement