【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
相关推荐
Olivia0514051417 分钟前
Voohu:CHIP LAN在2.5G/5G多速率以太网中的共模抑制带宽要求与选型
网络
缝艺智研社1 小时前
2026年 自动化缝纫模板机 机器人工作站市场洞察与排名
大数据·网络·人工智能·自动化·制造·新人首发·自动化缝纫机
静心观复1 小时前
从短连接到 gRPC:一文读懂 HTTP 连接模型的演进
网络·网络协议·http
哼?~1 小时前
应用层协议TCP
网络·tcp
xhbh6662 小时前
Win系统实现网络转发与端口映射:从 IPEnableRouter 到 RRAS 完整步骤
网络·端口转发·流量端口转发·ssh端口转发·端口转发工具
原来是猿2 小时前
TCP Echo Server 深度解析:从单进程到线程池的演进之路(上)
服务器·网络·tcp/ip
小新同学^O^2 小时前
简单学习 --> TCP协议
java·网络·tcp
其实防守也摸鱼3 小时前
软件安全与漏洞--软件安全设计
运维·网络·安全·网络安全·密码学·需求分析·软件安全
一只数据集3 小时前
NVIDIA Nemotron AIQ Agentic Safety Dataset:面向企业级智能体系统的安全与防护评估数据集全面解析
网络·数据库·安全
你的保护色3 小时前
软件定义网络SDN
网络