Search code examples
automationansiblecisco-ios

Ansible cisco ios, change vlan on interface


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 ?


Solution

  • 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 }}"