【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
相关推荐
什么鬼昵称1 小时前
Pikachu-xxe-xxe漏洞
网络·安全·xxe
IG工程师2 小时前
关于 S7 - 1200 通过存储卡进行程序更新
经验分享·笔记·自动化
真正的醒悟4 小时前
资源网站分享
网络
神一样的老师4 小时前
面向MQTT基础物联网网络的Age-of-Information感知的保留消息策略
网络·物联网
中草药z5 小时前
【JavaEE】http/https 超级详解
网络·笔记·网络协议·学习·http·https·计网
怒放的生命.5 小时前
电器自动化入门08:隔离变压器、行程开关介绍及选型
运维·自动化·电气自动化·电工基础
我爱学Python!5 小时前
基于 LangChain 的自动化测试用例的生成与执行
人工智能·自然语言处理·langchain·自动化·llm·测试用例·大语言模型
huaqianzkh5 小时前
付费计量系统通用功能(13)
网络·安全·架构
丶21365 小时前
【网络】网络安全概述
网络