目录
R2的脚本:R2Config.yml
R3的脚本:R3Config.yml
所有Routers脚本整合到一起:RoutersConfig.yml
R2的脚本:R2Config.yml
---
- name: R2 Configuration
hosts: R2
gather_facts: false
connection: network_cli
tasks:
- name: Interface Config
ios_config:
parents: "interface {{ item.interface }}"
lines:
- "encapsulation dot1Q {{ item.vlan }}"
- "ip address {{ item.address }} 255.255.255.0"
with_items:
- { interface : FastEthernet2/0.40, vlan : 40, address : 192.168.40.1 }
- { interface : FastEthernet2/0.50, vlan : 50, address : 192.168.50.1 }
- { interface : FastEthernet2/0.60, vlan : 60, address : 192.168.60.1 }
register: print_output
- debug: var=print_output
- name: DHCP Config
ios_config:
parents: "ip dhcp pool {{ item.pool }}"
lines:
- "network {{ item.network }} 255.255.255.0"
- "default-router {{ item.def_dns }}"
- "dns-server {{ item.def_dns }}"
with_items:
- { pool : Finance, network : 192.168.40.0, def_dns : 192.168.40.1 }
- { pool : HR, network : 192.168.50.0, def_dns : 192.168.50.1 }
- { pool : Sales, network : 192.168.60.0, def_dns : 192.168.60.1 }
register: print_output
- debug: var=print_output
- name: OSPF Config
ios_config:
parents: "router ospf 10"
lines:
- network 192.168.40.0 0.0.0.255 area 0
- network 192.168.50.0 0.0.0.255 area 0
- network 192.168.60.0 0.0.0.255 area 0
register: print_output
- debug: var=print_output
R3的脚本:R3Config.yml
---
- name: R3 Configuration
hosts: R3
gather_facts: false
connection: network_cli
tasks:
- name: Interface Config
ios_config:
parents: "interface {{ item.interface }}"
lines:
- "encapsulation dot1Q {{ item.vlan }}"
- "ip address {{ item.address }} 255.255.255.0"
with_items:
- { interface : FastEthernet2/0.70, vlan : 70, address : 192.168.70.1 }
- { interface : FastEthernet2/0.80, vlan : 80, address : 192.168.80.1 }
register: print_output
- debug: var=print_output
- name: DHCP Config
ios_config:
parents: "ip dhcp pool {{ item.pool }}"
lines:
- "network {{ item.network }} 255.255.255.0"
- "default-router {{ item.def_dns }}"
- "dns-server {{ item.def_dns }}"
with_items:
- { pool : IT, network : 192.168.70.0, def_dns : 192.168.70.1 }
- { pool : Admin, network : 192.168.80.0, def_dns : 192.168.80.1 }
register: print_output
- debug: var=print_output
- name: OSPF Config
ios_config:
parents: "router ospf 10"
lines:
- network 192.168.70.0 0.0.0.255 area 0
- network 192.168.80.0 0.0.0.255 area 0
register: print_output
- debug: var=print_output
所有Routers脚本整合到一起:RoutersConfig.yml
---
- name: Routers Global Configuration
hosts: Routers
gather_facts: false
connection: network_cli
tasks:
- name: DHCP enable
ios_config:
lines:
- service dhcp
register: print_output
- debug: var=print_output
- name: R1 Configuration
hosts: R1
gather_facts: false
connection: network_cli
tasks:
- name: Interface Config
ios_config:
parents: "interface {{ item.interface }}"
lines:
- "encapsulation dot1Q {{ item.vlan }}"
- "ip address {{ item.address }} 255.255.255.0"
with_items:
- { interface : FastEthernet2/0.10, vlan : 10, address : 192.168.10.1 }
- { interface : FastEthernet2/0.20, vlan : 20, address : 192.168.20.1 }
- { interface : FastEthernet2/0.30, vlan : 30, address : 192.168.30.1 }
register: print_output
- debug: var=print_output
- name: DHCP Config
ios_config:
parents: "ip dhcp pool {{ item.pool }}"
lines:
- "network {{ item.network }} 255.255.255.0"
- "default-router {{ item.def_dns }}"
- "dns-server {{ item.def_dns }}"
with_items:
- { pool : Reception, network : 192.168.10.0, def_dns : 192.168.10.1 }
- { pool : Store, network : 192.168.20.0, def_dns : 192.168.20.1 }
- { pool : Logistics, network : 192.168.30.0, def_dns : 192.168.30.1 }
register: print_output
- debug: var=print_output
- name: OSPF Config
ios_config:
parents: "router ospf 10"
lines:
- network 192.168.10.0 0.0.0.255 area 0
- network 192.168.20.0 0.0.0.255 area 0
- network 192.168.30.0 0.0.0.255 area 0
register: print_output
- debug: var=print_output
- name: R2 Configuration
hosts: R2
gather_facts: false
connection: network_cli
tasks:
- name: Interface Config
ios_config:
parents: "interface {{ item.interface }}"
lines:
- "encapsulation dot1Q {{ item.vlan }}"
- "ip address {{ item.address }} 255.255.255.0"
with_items:
- { interface : FastEthernet2/0.40, vlan : 40, address : 192.168.40.1 }
- { interface : FastEthernet2/0.50, vlan : 50, address : 192.168.50.1 }
- { interface : FastEthernet2/0.60, vlan : 60, address : 192.168.60.1 }
register: print_output
- debug: var=print_output
- name: DHCP Config
ios_config:
parents: "ip dhcp pool {{ item.pool }}"
lines:
- "network {{ item.network }} 255.255.255.0"
- "default-router {{ item.def_dns }}"
- "dns-server {{ item.def_dns }}"
with_items:
- { pool : Finance, network : 192.168.40.0, def_dns : 192.168.40.1 }
- { pool : HR, network : 192.168.50.0, def_dns : 192.168.50.1 }
- { pool : Sales, network : 192.168.60.0, def_dns : 192.168.60.1 }
register: print_output
- debug: var=print_output
- name: OSPF Config
ios_config:
parents: "router ospf 10"
lines:
- network 192.168.40.0 0.0.0.255 area 0
- network 192.168.50.0 0.0.0.255 area 0
- network 192.168.60.0 0.0.0.255 area 0
register: print_output
- debug: var=print_output
- name: R3 Configuration
hosts: R3
gather_facts: false
connection: network_cli
tasks:
- name: Interface Config
ios_config:
parents: "interface {{ item.interface }}"
lines:
- "encapsulation dot1Q {{ item.vlan }}"
- "ip address {{ item.address }} 255.255.255.0"
with_items:
- { interface : FastEthernet2/0.70, vlan : 70, address : 192.168.70.1 }
- { interface : FastEthernet2/0.80, vlan : 80, address : 192.168.80.1 }
register: print_output
- debug: var=print_output
- name: DHCP Config
ios_config:
parents: "ip dhcp pool {{ item.pool }}"
lines:
- "network {{ item.network }} 255.255.255.0"
- "default-router {{ item.def_dns }}"
- "dns-server {{ item.def_dns }}"
with_items:
- { pool : IT, network : 192.168.70.0, def_dns : 192.168.70.1 }
- { pool : Admin, network : 192.168.80.0, def_dns : 192.168.80.1 }
register: print_output
- debug: var=print_output
- name: OSPF Config
ios_config:
parents: "router ospf 10"
lines:
- network 192.168.70.0 0.0.0.255 area 0
- network 192.168.80.0 0.0.0.255 area 0
register: print_output
- debug: var=print_output