Guest User

Untitled

a guest
Apr 13th, 2021
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.12 KB | None | 0 0
  1. Playbook:
  2. ---
  3. - name: Do some things with Ansible and TTP
  4. hosts: localhost
  5. gather_facts: false
  6.  
  7. tasks:
  8. - name: "Pass text and command"
  9. ansible.utils.cli_parse:
  10. text: "{{ lookup('ansible.builtin.file', 'example.conf') }}"
  11. parser:
  12. name: ansible.utils.ttp
  13. template_path: "example.ttp"
  14. set_fact: interfaces_to_shut
  15.  
  16. - name: Generate config
  17. ansible.builtin.template:
  18. src: ports_to_shut.j2
  19. dest: rendered.config
  20.  
  21.  
  22. CLI output (show int status):
  23. ------------------------------------------------------------
  24. Port Name Status Vlan Duplex Speed Type
  25. Gi1/0/1 connected 100 a-full a-100 10/100/1000BaseTX
  26. Gi1/0/2 connected 100 a-full a-100 10/100/1000BaseTX
  27. Gi1/0/3 connected 100 a-full a-1000 10/100/1000BaseTX
  28. Gi1/0/4 connected 999 a-full a-1000 10/100/1000BaseTX
  29. Gi1/0/5 connected 999 a-full a-100 10/100/1000BaseTX
  30. Gi1/0/6 connected 999 a-full a-1000 10/100/1000BaseTX
  31. Gi1/0/7 connected 999 a-full a-1000 10/100/1000BaseTX
  32. Gi1/0/8 connected 999 a-full a-100 10/100/1000BaseTX
  33. Gi1/0/9 connected 999 a-full a-1000 10/100/1000BaseTX
  34. Gi1/0/10 connected 999 a-full a-100 10/100/1000BaseTX
  35. Gi1/0/11 notconnect 999 auto auto 10/100/1000BaseTX
  36. Gi1/0/12 notconnect 999 auto auto 10/100/1000BaseTX
  37. Gi1/0/13 notconnect 1 auto auto 10/100/1000BaseTX
  38. Gi1/0/14 notconnect 1 auto auto 10/100/1000BaseTX
  39. Gi1/0/15 notconnect 1 auto auto 10/100/1000BaseTX
  40. Gi1/0/16 notconnect 1 auto auto 10/100/1000BaseTX
  41. Gi1/0/17 notconnect 1 auto auto 10/100/1000BaseTX
  42. Gi1/0/18 notconnect 1 auto auto 10/100/1000BaseTX
  43. Gi1/0/19 notconnect 1 auto auto 10/100/1000BaseTX
  44. Gi1/0/20 notconnect 1 auto auto 10/100/1000BaseTX
  45. Gi1/0/21 notconnect 1 auto auto 10/100/1000BaseTX
  46. Gi1/0/22 notconnect 1 auto auto 10/100/1000BaseTX
  47. Gi1/0/23 notconnect 1 auto auto 10/100/1000BaseTX
  48. Gi1/0/24 connected routed a-half a-100 10/100/1000BaseTX
  49. Gi1/1/1 notconnect routed auto auto unknown
  50. Gi1/1/2 notconnect 1 auto auto unknown
  51. Gi1/1/3 notconnect 1 auto auto unknown
  52.  
  53.  
  54. CLI parser (example.ttp file template)
  55. ------------------------------------------------------------
  56. {{ interface }} {{ ignore('.*') }} {{ status }} {{ vlan | DIGIT }} {{ duplex }} {{ speed }} {{ type }}
  57.  
  58.  
  59. Jinja template:
  60. ------------------------------------------------------------
  61. {% for interface in ansible_facts['interfaces_to_shut'][0][0] %}
  62. {% if interface['vlan'] == "1" and interface['status'] == "notconnect" %}
  63. interface {{ interface['interface'] }}
  64. description "Unused (notconnect) port in VLAN 1 detected - Ansible shutting it down!"
  65. shutdown
  66. !
  67. {% endif %}
  68. {% endfor %}
  69.  
  70.  
  71. Rendered output:
  72. ------------------------------------------------------------
  73. interface Gi1/0/13
  74. description "Unused (notconnect) port in VLAN 1 detected - Ansible shutting it down!"
  75. shutdown
  76. !
  77. interface Gi1/0/14
  78. description "Unused (notconnect) port in VLAN 1 detected - Ansible shutting it down!"
  79. shutdown
  80. !
  81. interface Gi1/0/15
  82. description "Unused (notconnect) port in VLAN 1 detected - Ansible shutting it down!"
  83. shutdown
  84. !
  85. interface Gi1/0/16
  86. description "Unused (notconnect) port in VLAN 1 detected - Ansible shutting it down!"
  87. shutdown
  88. !
  89. interface Gi1/0/17
  90. description "Unused (notconnect) port in VLAN 1 detected - Ansible shutting it down!"
  91. shutdown
  92. !
  93. interface Gi1/0/18
  94. description "Unused (notconnect) port in VLAN 1 detected - Ansible shutting it down!"
  95. shutdown
  96. !
  97. interface Gi1/0/19
  98. description "Unused (notconnect) port in VLAN 1 detected - Ansible shutting it down!"
  99. shutdown
  100. !
  101. interface Gi1/0/20
  102. description "Unused (notconnect) port in VLAN 1 detected - Ansible shutting it down!"
  103. shutdown
  104. !
  105. interface Gi1/0/21
  106. description "Unused (notconnect) port in VLAN 1 detected - Ansible shutting it down!"
  107. shutdown
  108. !
  109. interface Gi1/0/22
  110. description "Unused (notconnect) port in VLAN 1 detected - Ansible shutting it down!"
  111. shutdown
  112. !
  113. interface Gi1/0/23
  114. description "Unused (notconnect) port in VLAN 1 detected - Ansible shutting it down!"
  115. shutdown
  116. !
  117. interface Gi1/1/2
  118. description "Unused (notconnect) port in VLAN 1 detected - Ansible shutting it down!"
  119. shutdown
  120. !
  121. interface Gi1/1/3
  122. description "Unused (notconnect) port in VLAN 1 detected - Ansible shutting it down!"
  123. shutdown
  124. !
  125.  
Advertisement
Add Comment
Please, Sign In to add comment