【网工Ansible】第 7 节: Ansible Quick Start: Playbooks and Cisco IOS Config module

一、预配置与课外知识

1.拓扑设备于配置导入

注意事项:记得使用给出的设备配置文件进行粘贴时,要复制到“wr”下后一空行处,否则“wr”命令将保持输入而未能执行

2.课外知识:cisco中 logging synchronous是干什么用的?

开启日志同步,如果不敲这条命令,自动弹出日志的时候如果你在配置设备,你就会发现日志信息会把你敲入的命令分隔开,影响输入。敲上这条命令之后日志信息不会分隔你敲到一半的命令行。

 二、源代码(可参考修改以自用)

由于按照教程示例拓扑的部署,似乎因交换机的spanning tree收敛而导致CPU hog,最终造成设备不可ping通而下发失败,因此未遵照后续案例跟做。能够参考其代码进行自己的命令编写与下发即可,其源代码如下:

---
- name: Cisco Wired Campus Design - Access
  hosts: gns3-access
  gather_facts: false
  connection: network_cli

  tasks:
    - name: Global config settings
      ios_config:
        lines:
          - vtp mode transparent
          - spanning-tree mode rapid-pvst
          - udld enable
          - errdisable recovery cause all
          - port-channel load-balance src-dst-ip
          - ip name-server 8.8.8.8
          - no ip http server
          - ip http secure-server

          - snmp-server community python1 RO
          - snmp-server community python2 RW

          - ntp server 87.81.181.2
          - ntp update-calendar

          - clock timezone PST -8
          - clock summer-time PDT recurring
          - service timestamps debug datetime msec localtime
          - service timestamps log datetime msec localtime

          - "vlan 1,10,20,30,40,50"
          - ip default-gateway 192.168.122.1
          - "ip dhcp snooping vlan 100,101"

          - no ip dhcp snooping information option 
          - ip dhcp snooping
          - "ip arp inspection vlan 100,101"
          - spanning-tree portfast bpduguard default


      register: print_output

    -  debug: var=print_output



    - name: Interface settings
      ios_config:
        parents: "interface {{ item.interface }}"
        lines:
          - "switchport access vlan {{ item.vlan }}"
          - "switchport voice vlan {{ item.voice_vlan }}"
          - switchport host
          - switchport port-security maximum 2 
          - switchport port-security
          - switchport port-security aging time 2
          - switchport port-security aging type inactivity 
          - switchport port-security violation restrict 
          - ip arp inspection limit rate 100
          - ip dhcp snooping limit rate 100
          - ip verify source
        after:
          - no shutdown
      with_items:
        - { interface : GigabitEthernet1/0, vlan : 100, voice_vlan : 101 }
        - { interface : GigabitEthernet1/1, vlan : 10, voice_vlan : 101 }
        - { interface : GigabitEthernet1/2, vlan : 20, voice_vlan : 101 }
        - { interface : GigabitEthernet1/3, vlan : 30, voice_vlan : 101 }

      register: print_output

    -  debug: var=print_output



- name: Cisco Wired Campus Design - Core
  hosts: gns3-core
  gather_facts: false
  connection: network_cli

  tasks:
    - name: Global config settings
      ios_config:
        lines:
          - vtp mode transparent
          - spanning-tree mode rapid-pvst
          - udld enable
          - errdisable recovery cause all
          - port-channel load-balance src-dst-ip
          - ip name-server 8.8.8.8
          - no ip http server
          - ip http secure-server

          - snmp-server community python1 RO
          - snmp-server community python2 RW

          - ntp server 87.81.181.2
          - ntp update-calendar

          - clock timezone PST -8
          - clock summer-time PDT recurring
          - service timestamps debug datetime msec localtime
          - service timestamps log datetime msec localtime

          - "vlan 1,10,20,30,40,50"
          - ip default-gateway 192.168.122.1
          - "ip dhcp snooping vlan 100,101"

          - no ip dhcp snooping information option 
          - ip dhcp snooping
          - "ip arp inspection vlan 100,101"
          - spanning-tree portfast bpduguard default


      register: print_output

    -  debug: var=print_output


三、照例实施记录

1.验证

1)sh run检索输出

最后一行命令并未出现在“资源”所给出的配置中,看情况决定后面是否再手动添加

2)sh spanning-tree输出

2.问题与处理

1)遇到问题

2)探索尝试

1-尝试添加( 参考:讲座问题

  1. [persistent_connection]
  2. connect_timeout = 60

2-尝试添加(  参考:讲座问答

最后一行命令:interpreter_python = auto_legacy_silent

(此步后,三个交换机中的一个实施成功!然后其他交换机上出现了【The error was: ansible.module_utils.connection.ConnectionError: timeout value 30 seconds reached while trying to send command:】)

3-尝试添加( 参考:思科社区

[persistent_connection]
command_timeout = 240

(三个都显示成功!!!但有两个交换机却存在命令丢失的问题)

3)最终解决(附代码及问题原因)

1-配置

如此设置

2-问题原因

命令丢失的原因找到:(讲座问答

1.确保您的网络自动化节点能够以低延迟 ping 到您的交换机,并且您的 GNS3vm CPU 使用率不是 100%。我发现 IOSvL2 15.2.4055 映像有时会导致此问题(GNS3vm CPU 利用率高),我更喜欢使用此版本,vios_l2-adventerprisek9-m.03.2017.qcow2。干杯。

2.任何可以触发多个交换机之间的生成树重新收敛的剧本都可能导致连接丢失,我喜欢这样。解决方法是使用 L3 OOB 管理网络,而不是 L2 OOB 管理网络。那,或者大大增加超时值来考虑db或stp重新收敛。

(为进行以上解决方案的尝试,在继续跟着教程查看被控主机配置时,发现下发的命令似乎都完整地存在于交换机上,奇怪)

四、Ansible剧本相关知识

一个yml文件一个playbook,包含多个任务(task),

Ansible 允许您在应用某些配置之前和应用某些配置之后执行一些操作。(见下例“after”)

关于配置内容与实施:

“这只是一个例子。您可能不想在设备上应用所有这些配置。”

“Ansible 使用多处理,因此您不会看到对多个设备进行配置会存在处理时间的巨大差异。”

“如果编写正确,多次运行剧本不会产生任何负面影响。”

  • 15
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值