Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Playbook:
- ---
- - name: Do some things with Ansible and TTP
- hosts: localhost
- gather_facts: false
- tasks:
- - name: "Pass text and command"
- ansible.utils.cli_parse:
- text: "{{ lookup('ansible.builtin.file', 'example.conf') }}"
- parser:
- name: ansible.utils.ttp
- template_path: "example.ttp"
- set_fact: interfaces_to_shut
- - name: Generate config
- ansible.builtin.template:
- src: ports_to_shut.j2
- dest: rendered.config
- CLI output (show int status):
- ------------------------------------------------------------
- Port Name Status Vlan Duplex Speed Type
- Gi1/0/1 connected 100 a-full a-100 10/100/1000BaseTX
- Gi1/0/2 connected 100 a-full a-100 10/100/1000BaseTX
- Gi1/0/3 connected 100 a-full a-1000 10/100/1000BaseTX
- Gi1/0/4 connected 999 a-full a-1000 10/100/1000BaseTX
- Gi1/0/5 connected 999 a-full a-100 10/100/1000BaseTX
- Gi1/0/6 connected 999 a-full a-1000 10/100/1000BaseTX
- Gi1/0/7 connected 999 a-full a-1000 10/100/1000BaseTX
- Gi1/0/8 connected 999 a-full a-100 10/100/1000BaseTX
- Gi1/0/9 connected 999 a-full a-1000 10/100/1000BaseTX
- Gi1/0/10 connected 999 a-full a-100 10/100/1000BaseTX
- Gi1/0/11 notconnect 999 auto auto 10/100/1000BaseTX
- Gi1/0/12 notconnect 999 auto auto 10/100/1000BaseTX
- Gi1/0/13 notconnect 1 auto auto 10/100/1000BaseTX
- Gi1/0/14 notconnect 1 auto auto 10/100/1000BaseTX
- Gi1/0/15 notconnect 1 auto auto 10/100/1000BaseTX
- Gi1/0/16 notconnect 1 auto auto 10/100/1000BaseTX
- Gi1/0/17 notconnect 1 auto auto 10/100/1000BaseTX
- Gi1/0/18 notconnect 1 auto auto 10/100/1000BaseTX
- Gi1/0/19 notconnect 1 auto auto 10/100/1000BaseTX
- Gi1/0/20 notconnect 1 auto auto 10/100/1000BaseTX
- Gi1/0/21 notconnect 1 auto auto 10/100/1000BaseTX
- Gi1/0/22 notconnect 1 auto auto 10/100/1000BaseTX
- Gi1/0/23 notconnect 1 auto auto 10/100/1000BaseTX
- Gi1/0/24 connected routed a-half a-100 10/100/1000BaseTX
- Gi1/1/1 notconnect routed auto auto unknown
- Gi1/1/2 notconnect 1 auto auto unknown
- Gi1/1/3 notconnect 1 auto auto unknown
- CLI parser (example.ttp file template)
- ------------------------------------------------------------
- {{ interface }} {{ ignore('.*') }} {{ status }} {{ vlan | DIGIT }} {{ duplex }} {{ speed }} {{ type }}
- Jinja template:
- ------------------------------------------------------------
- {% for interface in ansible_facts['interfaces_to_shut'][0][0] %}
- {% if interface['vlan'] == "1" and interface['status'] == "notconnect" %}
- interface {{ interface['interface'] }}
- description "Unused (notconnect) port in VLAN 1 detected - Ansible shutting it down!"
- shutdown
- !
- {% endif %}
- {% endfor %}
- Rendered output:
- ------------------------------------------------------------
- interface Gi1/0/13
- description "Unused (notconnect) port in VLAN 1 detected - Ansible shutting it down!"
- shutdown
- !
- interface Gi1/0/14
- description "Unused (notconnect) port in VLAN 1 detected - Ansible shutting it down!"
- shutdown
- !
- interface Gi1/0/15
- description "Unused (notconnect) port in VLAN 1 detected - Ansible shutting it down!"
- shutdown
- !
- interface Gi1/0/16
- description "Unused (notconnect) port in VLAN 1 detected - Ansible shutting it down!"
- shutdown
- !
- interface Gi1/0/17
- description "Unused (notconnect) port in VLAN 1 detected - Ansible shutting it down!"
- shutdown
- !
- interface Gi1/0/18
- description "Unused (notconnect) port in VLAN 1 detected - Ansible shutting it down!"
- shutdown
- !
- interface Gi1/0/19
- description "Unused (notconnect) port in VLAN 1 detected - Ansible shutting it down!"
- shutdown
- !
- interface Gi1/0/20
- description "Unused (notconnect) port in VLAN 1 detected - Ansible shutting it down!"
- shutdown
- !
- interface Gi1/0/21
- description "Unused (notconnect) port in VLAN 1 detected - Ansible shutting it down!"
- shutdown
- !
- interface Gi1/0/22
- description "Unused (notconnect) port in VLAN 1 detected - Ansible shutting it down!"
- shutdown
- !
- interface Gi1/0/23
- description "Unused (notconnect) port in VLAN 1 detected - Ansible shutting it down!"
- shutdown
- !
- interface Gi1/1/2
- description "Unused (notconnect) port in VLAN 1 detected - Ansible shutting it down!"
- shutdown
- !
- interface Gi1/1/3
- description "Unused (notconnect) port in VLAN 1 detected - Ansible shutting it down!"
- shutdown
- !
Advertisement
Add Comment
Please, Sign In to add comment