1

Ansible cisco ios, change vlan on interface

I am just a beginner (ansible 2.7.7) and I still have to study a lot of literature, but I hope in the future I will be able to learn it completely

Now I'm trying to write the following in the playbook

On ports that are in the state “line protocol is down” The number of received or transmitted packets is 0

Run the command on the port "switchport access vlan 537"

I can get the port status in ios_facts, but there is no information about the counter Can you please tell me on the playbook? how can i implement it?

- name: Collect IOS facts
  hosts: ciscoswitch

  tasks:

    - name: Facts
      ios_command:
        commands: show interfaces counters | i 0              0
      register: ios_comm_result

it view:

{
    "changed": false,
    "failed": false,
    "stdout": [
        "Fa0/6                  0              0              0              0 \nFa0/7                  0              0              0              0 \nFa0/8                  0              0              0              0 \nGi0/2                  0              0              0              0 \nFa0/6                  0              0              0              0 \nFa0/7                  0              0              0              0 \nFa0/8                  0              0              0              0 \nGi0/2                  0              0              0              0"
    ],
    "stdout_lines": [
        [
            "Fa0/6                  0              0              0              0 ",
            "Fa0/7                  0              0              0              0 ",
            "Fa0/8                  0              0              0              0 ",
            "Gi0/2                  0              0              0              0 ",
            "Fa0/6                  0              0              0              0 ",
            "Fa0/7                  0              0              0              0 ",
            "Fa0/8                  0              0              0              0 ",
            "Gi0/2                  0              0              0              0"
        ]
    ]
}

How can i parsing register ios_comm_result and send command to change port in register result ?

Artur88
  • 43
  • 6

2 Answers2

2

Post Tom Klimek - is correct but have some little mistake in sintax That correct:

- name: Collect IOS facts
  hosts: Switch

  tasks:

    - name: Facts
      ios_command:
        commands: show interfaces counters | i 0              0
      register: ios_comm_result

    - name: get a list of ports to modify
      set_fact:
        newlist: "{{(newlist | default([])) + [item.split()[0]]}}"
      with_items: "{{ios_comm_result.stdout_lines}}"

    - name: Apply vlan command
      ios_config:
        lines:
         - "sw acc vlan 998"
        parents: "int {{ item.0 }}"
      loop: "{{newlist | zip(newlist) | list }}"
Artur88
  • 43
  • 6
1

This is an older post so you probably figured it out already but here is an example of code:

 tasks:

 - name: Facts
   ios_command:
     commands: show interfaces counters | i 0              0
   register: ios_comm_result

 - name: get a list of ports to modify
    set_fact:
      newlist: "{{newlist + [item.split()[0]]}}"
    with_items: "{{ios_comm_result.stdout_lines}}"

- name: Apply vlan command
    ios_config:
      lines:
       - "sw acc vlan 537"
      parents: "int {{ item.0 }}"
    loop: "{{newlist|zip(nMatches)|list }}"
    
Tom Klimek
  • 11
  • 3