Guest User

Untitled

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