【GNS3 GraduProj】交换机Ansible脚本测试(文件备份)

为交换机S1创建vlan:S1VlanCreate.yml

(测试成功)

复制代码
---
- name: S1 Configuration
  hosts: S1
  gather_facts: false
  connection: network_cli

  tasks:
    - name: Interface Config
      ios_command:
        commands:
          - vlan database
          - vlan 10
          - vlan 20
          - vlan 30
          - exit

      register: print_output

    -  debug: var=print_output

为S1接口配置VLAN:S1InterVlan.yml

(测试成功,但显示change,因为在模拟交换机的运行配置中"switchport mode access"不会显示,因此总是会出现配置更新(即"change")的结果)

复制代码
---
- name: S1 Configuration
  hosts: S1
  gather_facts: false
  connection: network_cli
  
tasks:
    - name: Interface Config
      ios_config:
        parents: "interface {{ item.interface }}"
        lines:
          - switchport mode access
          - "switchport access vlan {{ item.vlan }}"
      with_items:
        - { interface : FastEthernet1/1, vlan : 10 }
        - { interface : FastEthernet1/2, vlan : 20 }
        - { interface : FastEthernet1/3, vlan : 30 }

      register: print_output

    -  debug: var=print_output

为所有交换机的f1/0接口配置trunk:S1InterTrunk.yml

(测试成功,同上,在模拟交换机配置中命令"switchport trunk encapsulation dot1q"不被显示(也可能是默认,而不支持),故删去此命令后无"change",而只显示"ok"。但最终因为查验交换机支持此条与上面那条命令,故都保存。)

复制代码
---
- name: Switches Global Configuration
  hosts: Switches
  gather_facts: false
  connection: network_cli

  tasks:
    - name: Interface Trunk Config
      ios_config:
        parents: "interface FastEthernet1/0"
        lines:
          - switchport mode trunk 

      register: print_output

    -  debug: var=print_output

以上三者合在一个剧本中:S1Switches.yml

复制代码
---
- name: S1 Configuration
  hosts: S1
  gather_facts: false
  connection: network_cli

  tasks:
    - name: Vlan Config
      ios_command:
        commands:
          - vlan database
          - vlan 10
          - vlan 20
          - vlan 30
          - exit

      register: print_output

    -  debug: var=print_output



    - name: Interface Acces Config
      ios_config:
        parents: "interface {{ item.interface }}"
        lines:
          - switchport mode access
          - "switchport access vlan {{ item.vlan }}"
      with_items:
        - { interface : FastEthernet1/1, vlan : 10 }
        - { interface : FastEthernet1/2, vlan : 20 }
        - { interface : FastEthernet1/3, vlan : 30 }

      register: print_output

    -  debug: var=print_output



- name: Switches Global Configuration
  hosts: Switches
  gather_facts: false
  connection: network_cli

  tasks:
    - name: Interface Trunk Config
      ios_config:
        parents: "interface FastEthernet1/0"
        lines:
          - switchport trunk encapsulation dot1q
          - switchport mode trunk 

      register: print_output

    -  debug: var=print_output
相关推荐
Rabitebla2 小时前
【Linux操作系统】 文件篇(一):文件描述符与 Linux 文件管理机制
linux·网络·c++·笔记·学习
manduic4 小时前
汽车座舱Buck选型实战:AWK6613 对比 SCT2432,参数逐项对齐(附选型清单)
网络·汽车
IT大白鼠5 小时前
当 AI 开始自动化渗透:FBI ARTEX 警报的防守对策
运维·人工智能·自动化
惜.己5 小时前
Java结合idea简单使用Playwright
java·开发语言·自动化
通信王669 小时前
NBIOT NTN协议整理-TS36.211
网络
光电笑映11 小时前
TCP 为什么可靠?从三次握手、滑动窗口到拥塞控制全面拆解(上)
运维·服务器·网络·udp
Vni11 小时前
华三双核心交换机IRF堆叠
网络·交换机·堆叠·华三
LSDlsdoo11 小时前
冷却液与油污飞溅工况下,国产接近开关怎么选?五步工程化选型方法
其他·自动化
15Moonlight12 小时前
Linux 网络(11):IO 多路转接(select /poll/epoll)
linux·网络
峥嵘life13 小时前
Android16 热点「网络共享」开关的设计与实现
服务器·网络·microsoft