【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
相关推荐
漫谈网络38 分钟前
基于 Netmiko 的网络设备自动化操作
运维·自动化·netdevops·netmiko
三思而后行,慎承诺1 小时前
tcp 和http 网络知识
网络·tcp/ip·http
JavaEdge.1 小时前
LangChain4j HTTP 客户端定制:解锁 LLM API 交互的更多可能性
网络·网络协议·http
Hy行者勇哥1 小时前
形象解释 HTTP 的四种常见请求方式及其中的区别联系
网络·网络协议·http
Cuit小唐1 小时前
TCP 协议:原理、机制与应用
网络·网络协议·tcp/ip
电鱼智能的电小鱼2 小时前
EFISH-SBC-RK3588无人机地面基准站项目
linux·网络·嵌入式硬件·机器人·无人机·边缘计算
电鱼智能的电小鱼2 小时前
基于 EFISH-SBC-RK3588 的无人机环境感知与数据采集方案
linux·网络·嵌入式硬件·数码相机·无人机·边缘计算
Arenaschi3 小时前
SQLite 是什么?
开发语言·网络·python·网络协议·tcp/ip
沧浪之水!4 小时前
【Linux网络】:套接字之UDP
linux·网络·udp
上海云盾-高防顾问4 小时前
高防IP+CDN组合:电商大促的“双保险”防护方案
网络·网络协议·tcp/ip