【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
相关推荐
lazy H1 小时前
【计算机网络】第 13 篇:NAT 和端口映射是什么?为什么本地服务外网访问不了?
java·网络·网络协议·学习·计算机网络·408
Linux运维技术栈1 小时前
WAF部署在防火墙后面,获取不到真实IP怎么办?
服务器·网络·tcp/ip·安全·防火墙·waf
广目软件1 小时前
HA分布式架构采集节点变更操作手册20261029
运维·服务器·ansible·zabbix·prometheus
实验室管理云平台2 小时前
面向高职院校实训场景:实训实验室管理平台整体方案分享
大数据·科技·物联网·自动化
是jin奥2 小时前
Claude Code + GitHub 自动化工作流整理
运维·自动化·github
垆边人似月.2 小时前
RDMA项目踩坑(一):为什么最后一块数据必须使用实际长度?
网络·c/c++·rdma·高性能io·tcp与rdma
YH_Analytics2 小时前
2026 高压 E-House 市场深度分析报告市场格局
网络·数据库
zl_dfq2 小时前
Windows 下 UDP 套接字编程指南
网络·windows·udp
日常筹谋记2 小时前
数据中心逆变设备运维|通讯对接、动环接入与多系统联动方案科普
运维·网络
PiaoKe___2 小时前
云手机技术解析:从 ARM 虚拟化到 Python 自动化实战
python·智能手机·自动化