【问题标题】:Ansible cisco ios, change vlan on interfaceAnsible cisco ios,改变接口上的vlan
【发布时间】:2021-08-02 01:00:19
【问题描述】:

Ansible cisco ios,改变接口上的vlan

我只是一个初学者(ansible 2.7.7),我还有很多文献要学习,但我希望将来我能完全学会它

现在我正在尝试在剧本中编写以下内容

在处于“线路协议已关闭”状态的端口上,接收或发送的数据包数为 0

在端口“switchport access vlan 537”上运行命令

我可以在 ios_facts 中获取端口状态,但是没有关于计数器的信息 能否请您在 playbook 上告诉我?我该如何实现它?

- name: Collect IOS facts
  hosts: ciscoswitch

  tasks:

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

它的视图:

{
    "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"
        ]
    ]
}

如何解析寄存器 ios_comm_result 并发送命令更改寄存器结果中的端口?

【问题讨论】:

    标签: automation ansible cisco-ios


    【解决方案1】:

    这是一篇较早的帖子,所以您可能已经猜到了,但这里是一个代码示例:

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

    【讨论】:

    • 这看起来不错,但可能需要一些解释。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-08
    相关资源
    最近更新 更多