Hey Habr!
เบเปเปเบเบปเบเบกเบฒเบเบตเป, เบเบปเบเบเบงเบฒเบกเปเบเปเบเบฒเบเบปเบเบขเบนเปเบเบตเป
เบเบตเปเบเปเปเปเบกเปเบเบฅเบปเบเบเบตเบเบเปเบฒเบญเบดเบเบชเปเบฒเบฅเบฑเบเบงเบฝเบเบเบฒเบเบเบฑเปเบเบเปเบฒเบง. เบเบฒเบเปเบฅเบทเบญเบเบเปเบฒเบญเบดเบเปเบเปเบเบทเบเบเบฐเบเบดเบเบฑเบเบซเบผเบฒเบเบเบตเบเปเบญเบเบซเบเปเบฒเบเบตเปเบเบฑเบเบเบทเบเปเบเบเปเบญเบเปเบ เบเบญเบเบฎเบฑเบเปเบเป เบฅเบธเปเบ 1.x.x. เบฅเบปเบเบเบตเบเบเปเปเบเปเบญเบเปเบเปเบเบณเปเบเป เปเบฅเบฐเปเบเบฒเบฐเบชเบฐเบเบฑเปเบเบเบถเปเบเปเบเบตเบ rusted เบขเบนเปเบชเบฐเปเปเบต. เปเบเบเบงเบฒเบกเบฎเบนเปเบชเบถเบเบงเปเบฒเบงเบฝเบเบเบฒเบเบเบปเบงเบกเบฑเบเปเบญเบเบเปเปเปเบเบตเบเบเบทเปเบเปเบฅเบทเปเบญเบเปเปเบเบปเปเบฒเบเบตเปเบชเบฐเบเบฑเบเบเบฑเบเบเบธเบ เบเบญเบเบฎเบฑเบเปเบเป. เปเบฅเบฐเบเบธเบเบเบฑเปเบเบเบตเปเปเบเบปเปเบฒเบเปเบญเบเบเบฒเบเบเบฑเบเบฅเบปเบ, เบฅเบฐเบเบปเบเบเปเบญเบเปเบชเปเบเบปเบเบซเบผเบทเบฅเปเปเบซเบผเบธเบเบฅเบปเบ. เบขเปเบฒเบเปเบเบเปเบเบฒเบก, เบเบฒเบเบชเปเบงเบเบเปเบฒเบญเบดเบ, เบเบฒเบเบชเปเบฒเบ configs, เบชเบฐเปเบซเบกเบตเปเบฎเบฑเบเบงเบฝเบเบขเปเบฒเบเบเบฑเบเปเบเบเบซเบผเบฒเบ, เปเบเบเบเบต jinja2 เปเบเบทเปเบญเบเบเบฑเบเปเบเปเบเบทเบเบชเปเบฒเบเบเบฑเปเบเบเบถเปเบเบเบปเบเบเบฒเบ. เปเบเปเบเบฒเบเบชเปเบงเบเบเบตเบชเบญเบ - rolling out configs - เบเบปเบเบเบฐเบเบดเปเบฅเปเบงเปเบเปเบเปเบฒเปเบญเบปเบฒเบเบงเบฒเบกเปเบเบเปเบ. เปเบฅเบฐเบเบฑเบเบเบฑเปเบเปเบเปเบเปเบญเบเบเปเบญเบเบกเปเบงเบ config เบซเปเบฒเบเปเบเบชเบญเบเบซเบผเบตเบเปเบเบดเบเปเบเบดเปเบเบซเบเบถเปเบเบฎเปเบญเบเบญเบธเบเบฐเบเบญเบ, เบเบฒเบเบเปเบญเบเบขเบนเปเปเบเบซเบฅเบฒเบเบเบฑเบเบเบดเปเบฅเปเบกเบฑเบ, เบเบฒเบเบเปเบฒเปเบเปเปเบเบทเปเบญเบเบกเบทเบเบตเปเปเบกเปเบเบซเบเปเบฒเปเบเบทเปเบญเปเบฅเบฑเบเบเปเบญเบ.
เปเบเบเบตเปเบเบตเปเบเปเบญเบเบเปเบญเบเบเบญเบกเบฎเบฑเบเบงเปเบฒเบเบงเบฒเบกเบเปเปเปเบเปเบเบญเบเบเบญเบเบเปเบญเบเบกเบฑเบเบเบฐเบขเบนเปเปเบเบเบฒเบเบเบฒเบเบเบงเบฒเบกเบเบธเปเบเปเบเบตเบเบเบญเบเบเปเบญเบ เบเบญเบเบฎเบฑเบเปเบเปเบเปเบงเบฒเบขเบนเปเปเบเบเปเปเบเบปเบเบเปเบญเบเบเบญเบเบกเบฑเบ. เปเบฅเบฐเบเบตเป, เปเบเบเบงเบดเบเบตเบเบฒเบเบเบฒเบ, เปเบเบฑเบเบเบธเบเบชเปเบฒเบเบฑเบ. เบเบญเบเบฎเบฑเบเปเบเป เปเบกเปเบเปเบเบเบเปเบฒเบเบซเบฒเบเบขเปเบฒเบเบชเบปเบกเบเบนเบ, เบเบทเปเบเบเบตเปเบเบงเบฒเบกเบฎเบนเปเบเบญเบเบเบปเบเปเบญเบเบเบฑเบ DSL (เบเบฒเบชเบฒเบชเบฐเปเบเบฒเบฐเบเบญเบเปเบเปเบกเบ), เปเบเบดเปเบเบเปเบญเบเปเบเปเบฎเบฑเบเบเบฒเบเบฎเบฑเบเบชเบฒเปเบงเปเปเบเบฅเบฐเบเบฑเบเบเบงเบฒเบกเบซเบกเบฑเปเบเปเบ. เปเบฅเปเบง, เปเบงเบฅเบฒเบเบฑเปเบ เบเบญเบเบฎเบฑเบเปเบเป เบกเบฑเบเบเปเบฒเบฅเบฑเบเบเบฑเบเบเบฐเบเบฒเบขเปเบฒเบเปเบงเบงเบฒ, เปเบฅเบฐเปเบเบเบเปเปเบกเบตเบเบฒเบเบเบดเบเบฒเบฅเบฐเบเบฒเบเบดเปเบชเบเบชเปเบฒเบฅเบฑเบเบเบฒเบเปเบเบปเปเบฒเบเบฑเบเปเบเปเปเบเบเปเบฒเบเบซเบฅเบฑเบ, เบกเบฑเบเบเปเปเปเบเปเปเบเบตเปเบกเบเบงเบฒเบกเบซเบกเบฑเปเบเปเบ.
เบเบฑเปเบเบเบฑเปเบ, เบเปเปเบเบปเบเบเปเบญเบเบซเบเปเบฒเบเบตเป, เบฅเบปเบเบเบตเบเบฎเบธเปเบเบเบตเบชเบญเบเปเบเปเบเบทเบเบเบฐเบเบดเบเบฑเบ. เปเบงเบฅเบฒเบเบตเป python, เบซเบผเบทเปเบเบเบเบตเปเบเบฐเบขเบนเปเปเบเบเบญเบเบเบตเปเบเบฝเบเปเบงเปเปเบ python เปเบฅเบฐ เบชเบณ เบฅเบฑเบ python เบเบฒเบเปเบเปเบเบทเป
เบเบฑเปเบเบเบฑเปเบ - เบเปเปเบเบต เปเบเบฑเบ microframework เบฅเบฒเบเบฅเบฑเบเบญเบฑเบเบชเบญเบเปเบ python เปเบฅเบฐ เบชเบณ เบฅเบฑเบ python เปเบฅเบฐโเบเบทเบโเบญเบญเบโเปเบเบโเบชเปเบฒโเบฅเบฑเบโเบเบฒเบโเบญเบฑเบโเบเบฐโเปเบโเบกเบฑเบโ. เปเบเบฑเปเบเบเบฝเบงเบเบฑเบเบเบฑเบเบเปเบฅเบฐเบเบตเบเบตเปเบกเบต เบเบญเบเบฎเบฑเบเปเบเป, เปเบเบทเปเบญเปเบเปเปเบเบเบฑเบเบซเบฒเบขเบนเปเบเบตเปเบเบตเป, เบเบฒเบเบเบฐเบเบฝเบกเบเปเปเบกเบนเบเบเบตเปเบกเบตเบเบงเบฒเบกเบชเบฒเบกเบฒเบเปเบเบเบฒเบ, i.e. เบชเบดเบเบเปเบฒเบเบปเบเบเบฑเบเบเบญเบ hosts เปเบฅเบฐเบเบปเบงเบเปเบฒเบเบปเบเบเบฒเบเบเบญเบเปเบเบปเบฒเปเบเบปเปเบฒ, เปเบเป scripts เบเปเปเปเบเปเบเบฝเบเบขเบนเปเปเบ DSL เปเบเบเบเปเบฒเบเบซเบฒเบ, เปเบเปเปเบเบเบฝเบงเบเบฑเบเบเปเปเปเบเบปเปเบฒเบซเบผเบฒเบ, เปเบเปเบเบตเบซเบผเบฒเบ p[i|i]ton.
เปเบซเปเปเบเบดเปเบเบชเบดเปเบเบเบตเปเบกเบฑเบเปเบเปเบเบปเบงเบขเปเบฒเบเบชเบปเบเบเปเปเปเบเบเบตเป.
เบเปเบญเบเบกเบตเปเบเบทเบญเบเปเบฒเบเบชเบฒเบเบฒเบเบตเปเบกเบตเบซเปเบญเบเบเบฒเบเบซเบผเบฒเบเบชเบดเบเปเบซเปเบเปเบเบเบปเปเบงเบเบฐเปเบเบ. เปเบเปเบฅเบฐเบซเปเบญเบเบเบฒเบเบกเบต Router WAN เบเบตเปเบขเบธเบเบเปเบญเบเบเบฒเบเบเบฒเบเบชเบทเปเบชเบฒเบเบเบฒเบเบเบนเปเบเบฐเบเบดเบเบฑเบเบเบฒเบเบเบตเปเปเบเบเบเปเบฒเบเบเบฑเบ. เปเบเบฃเปเบเบเปเบเบฒเบเบเบณเบเบปเบเปเบชเบฑเปเบเบเบฒเบเปเบกเปเบ BGP. routers WAN เบกเบตเบชเบญเบเบเบฐเปเบเบ: Cisco ISG เบซเบผเบท Juniper SRX.
เปเบเบเบฑเบเบเบธเบเบฑเบเบงเบฝเบเบเบฒเบ: เบเปเบฒเบเบเปเบฒเปเบเบฑเบเบเปเบญเบเปเบเปเบเปเบฒเบซเบเบปเบเบเปเบฒเปเบเบทเบญเบเปเบฒเบเบเปเบญเบเบชเบฐเปเบเบฒเบฐเบชเปเบฒเบฅเบฑเบเบเบฒเบเปเบเบปเปเบฒเบฅเบฐเบงเบฑเบเบงเบดเบเบตเปเบญเบขเบนเปเปเบเบเบญเบเปเบเบเบเปเบฒเบเบซเบฒเบเบเปเบฝเบงเบเบฑเบ router WAN เบเบฑเบเบซเบกเบปเบเบเบญเบเปเบเบทเบญเบเปเบฒเบเบชเบฒเบเบฒ - เปเบเบชเบฐเบเบฒเปเบเบทเบญเบเปเบฒเบเบเปเบญเบเบเบตเปเปเบ BGP - เบเปเบฒเบเบปเบเบเบญเบเปเบเบเบเบงเบฒเบกเปเบงเบเบญเบเบเบญเบเบเบตเปเบญเบธเบเบดเบเบเบปเบ.
เบซเบเปเบฒเบเปเบฒเบญเบดเบ, เบเบงเบเปเบฎเบปเบฒเบเปเบฒเปเบเบฑเบเบเปเบญเบเปเบเปเบเบฐเบเบฝเบกเบเบนเปเบเปเบถเบเบญเบเปเบกเปเปเบเบ, เบเบปเบเบเบทเปเบเบเบฒเบเบเบญเบเบเบฒเบเบเบฑเปเบเบเปเบฒเบเบฐเบเบทเบเบชเปเบฒเบเบเบถเปเบเปเบเบเบเปเบฒเบเบซเบฒเบเบชเปเบฒเบฅเบฑเบ Cisco เปเบฅเบฐ Juniper. เบกเบฑเบเบเบฑเบเบกเบตเบเบงเบฒเบกเบเปเบฒเปเบเบฑเบเปเบเบเบฒเบเบเบฐเบเบฝเบกเบเปเปเบกเบนเบเบชเปเบฒเบฅเบฑเบเปเบเปเบฅเบฐเบเบธเบเปเบฅเบฐเบเบปเบงเบเปเบฒเบเบปเบเบเบฒเบเปเบเบทเปเบญเบกเบเปเป, i.e. เปเบเบฑเบเบเปเบฒเบเปเปเบกเบนเบเบเบปเบเบเบฑเบเบเบฝเบงเบเบฑเบ
เปเบกเปเปเบเบเบเปเบญเบกเบชเปเบฒเบฅเบฑเบ Cisco:
$ cat templates/ios/base.j2
class-map match-all VIDEO_SURV
match access-group 111
policy-map VIDEO_SURV
class VIDEO_SURV
police 1500000 conform-action transmit exceed-action drop
interface {{ host.task_data.ifname }}
description VIDEOSURV
ip address 10.10.{{ host.task_data.ipsuffix }}.254 255.255.255.0
service-policy input VIDEO_SURV
router bgp {{ host.task_data.asn }}
network 10.40.{{ host.task_data.ipsuffix }}.0 mask 255.255.255.0
access-list 11 permit 10.10.{{ host.task_data.ipsuffix }}.0 0.0.0.255
access-list 111 permit ip 10.10.{{ host.task_data.ipsuffix }}.0 0.0.0.255 any
เปเบกเปเปเบเบเบชเปเบฒเบฅเบฑเบ Juniper:
$ cat templates/junos/base.j2
set interfaces {{ host.task_data.ifname }} unit 0 description "Video surveillance"
set interfaces {{ host.task_data.ifname }} unit 0 family inet filter input limit-in
set interfaces {{ host.task_data.ifname }} unit 0 family inet address 10.10.{{ host.task_data.ipsuffix }}.254/24
set policy-options policy-statement export2bgp term 1 from route-filter 10.10.{{ host.task_data.ipsuffix }}.0/24 exact
set security zones security-zone WAN interfaces {{ host.task_data.ifname }}
set firewall policer policer-1m if-exceeding bandwidth-limit 1m
set firewall policer policer-1m if-exceeding burst-size-limit 187k
set firewall policer policer-1m then discard
set firewall policer policer-1.5m if-exceeding bandwidth-limit 1500000
set firewall policer policer-1.5m if-exceeding burst-size-limit 280k
set firewall policer policer-1.5m then discard
set firewall filter limit-in term 1 then policer policer-1.5m
set firewall filter limit-in term 1 then count limiter
เปเบกเปเปเบเบ, เปเบเปเบเบญเบ, เบเปเปเบญเบญเบเบกเบฒเบเบฒเบเบญเบฒเบเบฒเบเบเบฒเบเป. เปเบซเบผเบปเปเบฒเบเบตเปเปเบกเปเบเบเบงเบฒเบกเปเบเบเบเปเบฒเบเบเบตเปเบชเปเบฒเบเบฑเบเบฅเบฐเบซเบงเปเบฒเบเบเบฒเบเบเบฑเปเบเบเปเบฒเบเบฒเบเปเบฎเบฑเบเบงเบฝเบเบเบตเปเปเบเบฑเบเปเบฅเบฐเบซเบผเบฑเบเบเบฒเบเบเบฒเบเปเบเปเปเบเบงเบฝเบเบเบฒเบเปเบเบชเบญเบ routers เบชเบฐเปเบเบฒเบฐเบเบญเบเปเบเบเบเบตเปเปเบเบเบเปเบฒเบเบเบฑเบ.
เบเบฒเบเปเบกเปเปเบเบเบเบญเบเบเบงเบเปเบฎเบปเบฒเบเบงเบเปเบฎเบปเบฒเปเบซเบฑเบเบงเปเบฒเปเบเบทเปเบญเปเบเปเปเบเบเบฑเบเบซเบฒ, เบเบงเบเปเบฎเบปเบฒเบเปเบญเบเบเบฒเบเบเบฝเบเปเบเปเบชเบญเบเบเบปเบงเบเปเบฒเบเบปเบเบเบฒเบเบชเปเบฒเบฅเบฑเบ Juniper เปเบฅเบฐ 3 เบเบปเบงเบเปเบฒเบเบปเบเบเบฒเบเบชเปเบฒเบฅเบฑเบ Cisco. เบเบตเปเบเบงเบเปเบเบปเบฒเปเบกเปเบ:
- ifname
- ipsuffix
- asn
เปเบเบเบฑเบเบเบธเบเบฑเบเบเบงเบเปเบฎเบปเบฒเบเปเบฒเปเบเบฑเบเบเปเบญเบเปเบเปเบเปเบฒเบเบปเบเบเบปเบงเบเปเบฒเบเบปเบเบเบฒเบเปเบซเบผเบปเปเบฒเบเบตเปเบชเปเบฒเบฅเบฑเบเปเบเปเบฅเบฐเบญเบธเบเบฐเบเบญเบ, i.e. เปเบฎเบฑเบเบชเบดเปเบเบเบฝเบงเบเบฑเบ เบชเบดเบเบเปเบฒเบเบปเบเบเบฑเบ.
เปเบ เบชเบดเบเบเปเบฒเบเบปเบเบเบฑเบ เบเบงเบเปเบฎเบปเบฒเบเบฐเบเบฐเบเบดเบเบฑเบเบเบฒเบกเปเบญเบเบฐเบชเบฒเบเบขเปเบฒเบเปเบเบฑเปเบกเบเบงเบ
เบเบฑเปเบเปเบกเปเบ, เปเบซเปเบชเปเบฒเบเปเบเบเบเบฐเบเบนเบเปเบเบฅเปเบเบฝเบงเบเบฑเบ:
.
โโโ config.yaml
โโโ inventory
โ โโโ defaults.yaml
โ โโโ groups.yaml
โ โโโ hosts.yaml
เปเบเบฅเป config.yaml เปเบกเปเบเปเบเบฅเปเบเบฒเบเบเบฑเปเบเบเปเบฒเบกเบฒเบเบเบฐเบเบฒเบ nornir
$ cat config.yaml
---
core:
num_workers: 10
inventory:
plugin: nornir.plugins.inventory.simple.SimpleInventory
options:
host_file: "inventory/hosts.yaml"
group_file: "inventory/groups.yaml"
defaults_file: "inventory/defaults.yaml"
เบเบงเบเปเบฎเบปเบฒเบเบฐเบเบตเปเบเบญเบเบเบปเบงเบเปเบฒเบเบปเบเบเบฒเบเบเบปเปเบเบเปเปเบเปเบเบฅเป hosts.yaml, เบเบธเปเบก (เปเบเบเปเบฅเบฐเบเบตเบเบญเบเบเปเบญเบเปเบซเบผเบปเปเบฒเบเบตเปเปเบกเปเบเปเบเบปเปเบฒเบชเบนเปเบฅเบฐเบเบปเบ / เบฅเบฐเบซเบฑเบเบเปเบฒเบ) เปเบ เบเบธเปเบก.yaml, เปเบฅเบฐเปเบ defaults.yaml เบเบงเบเปเบฎเบปเบฒเบเบฐเบเปเปเบเบตเปเบเบญเบเบซเบเบฑเบ, เปเบเปเบเปเบฒเบเบเปเบฒเปเบเบฑเบเบเปเบญเบเปเบชเปเบชเบฒเบก minuses เบขเบนเปเบเบตเปเบเบฑเปเบ - เบเบตเปเปเบซเปเปเบซเบฑเบเบงเปเบฒเบกเบฑเบเปเบกเปเบ เบขเบญเบ เปเบเบฅเปเปเบกเปเบเบซเบงเปเบฒเบเปเบเบปเปเบฒ.
เบเบตเปเปเบกเปเบเบชเบดเปเบเบเบตเป hosts.yaml เปเบเบดเปเบเบเบทเบงเปเบฒ:
---
srx-test:
hostname: srx-test
groups:
- juniper
data:
task_data:
ifname: fe-0/0/2
ipsuffix: 111
cisco-test:
hostname: cisco-test
groups:
- cisco
data:
task_data:
ifname: GigabitEthernet0/1/1
ipsuffix: 222
asn: 65111
เปเบฅเบฐเบเบตเปเปเบกเปเบเบเบธเปเบก.yaml:
---
cisco:
platform: ios
username: admin1
password: cisco1
juniper:
platform: junos
username: admin2
password: juniper2
เบเบตเปเปเบกเปเบเบชเบดเปเบเบเบตเปเปเบเบตเบเบเบถเปเบ เบชเบดเบเบเปเบฒเบเบปเบเบเบฑเบ เบชเปเบฒเบฅเบฑเบเบงเบฝเบเบเบฒเบเบเบญเบเบเบงเบเปเบฎเบปเบฒ. เปเบโเบฅเบฐโเบซเบงเปเบฒเบโเบเบฒเบโเปเบฅเบตเปเบกโเบเบปเปเบโ, เบเบปเบงโเบเปเบฒโเบเบปเบโเบเบฒเบโเบเบฒเบโเปเบเบฅโเปโเบชเบฒเบโเปเบกเปเบโเบกเบตโเปเบเบโเบเบตเปโเบเบฑเบโเบฎเบนเบโเปเบเบโเบงเบฑเบโเบเบธโ InventoryElement.
เบเปเบฒเบเบฅเบธเปเบกเบเบตเป spoiler เปเบกเปเบเปเบเบเบงเบฒเบเบเบญเบเบเบปเบงเปเบเบ InventoryElement
print(json.dumps(InventoryElement.schema(), indent=4))
{
"title": "InventoryElement",
"type": "object",
"properties": {
"hostname": {
"title": "Hostname",
"type": "string"
},
"port": {
"title": "Port",
"type": "integer"
},
"username": {
"title": "Username",
"type": "string"
},
"password": {
"title": "Password",
"type": "string"
},
"platform": {
"title": "Platform",
"type": "string"
},
"groups": {
"title": "Groups",
"default": [],
"type": "array",
"items": {
"type": "string"
}
},
"data": {
"title": "Data",
"default": {},
"type": "object"
},
"connection_options": {
"title": "Connection_Options",
"default": {},
"type": "object",
"additionalProperties": {
"$ref": "#/definitions/ConnectionOptions"
}
}
},
"definitions": {
"ConnectionOptions": {
"title": "ConnectionOptions",
"type": "object",
"properties": {
"hostname": {
"title": "Hostname",
"type": "string"
},
"port": {
"title": "Port",
"type": "integer"
},
"username": {
"title": "Username",
"type": "string"
},
"password": {
"title": "Password",
"type": "string"
},
"platform": {
"title": "Platform",
"type": "string"
},
"extras": {
"title": "Extras",
"type": "object"
}
}
}
}
}
เบฎเบนเบเปเบเบเบเบตเปเบชเบฒเบกเบฒเบเปเบเบดเปเบเบชเบฑเบเบชเบปเบเปเบฅเบฑเบเบเปเบญเบ, เปเบเบเบชเบฐเปเบเบฒเบฐเปเบเบเบญเบเบเปเบฒเบญเบดเบ. เปเบเบเปเบฒเบชเบฑเปเบเบเบตเปเบเบฐเบเบดเบเบญเบญเบ, เบฎเบนเบเปเบเบเบเบฒเบเปเบเปเบเบญเบเปเบ python.
$ ipython3
Python 3.6.9 (default, Nov 7 2019, 10:44:02)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.1.1 -- An enhanced Interactive Python. Type '?' for help.
In [1]: from nornir import InitNornir
In [2]: nr = InitNornir(config_file="config.yaml", dry_run=True)
In [3]: nr.inventory.hosts
Out[3]:
{'srx-test': Host: srx-test, 'cisco-test': Host: cisco-test}
In [4]: nr.inventory.hosts['srx-test'].data
Out[4]: {'task_data': {'ifname': 'fe-0/0/2', 'ipsuffix': 111}}
In [5]: nr.inventory.hosts['srx-test']['task_data']
Out[5]: {'ifname': 'fe-0/0/2', 'ipsuffix': 111}
In [6]: nr.inventory.hosts['srx-test'].platform
Out[6]: 'junos'
เปเบฅเบฐเบชเบธเบเบเปเบฒเบ, เปเบซเปเปเบฎเบปเบฒเบเปเบฒเบงเปเบเบชเบนเปเบชเบฐเบเบฃเบดเบเปเบญเบ. เบเปเบฒเบเบฐเปเบเบปเปเบฒเบเปเปเบกเบตเบซเบเบฑเบเบเบตเปเบเบฐเบกเบตเบเบงเบฒเบกเบเบนเบกเปเบเปเบเบเบชเบฐเปเบเบฒเบฐเบขเบนเปเบเบตเปเบเบตเป. เบเปเบฒเบเบฐเปเบเบปเปเบฒเบเบฝเบเปเบเปเปเบญเบปเบฒเบเบปเบงเบขเปเบฒเบเบเบตเปเบเปเบญเบกเบเบตเปเบเบฐเปเบฎเบฑเบเบเบฒเบ
from nornir import InitNornir
from nornir.plugins.tasks import networking, text
from nornir.plugins.functions.text import print_title, print_result
def config_and_deploy(task):
# Transform inventory data to configuration via a template file
r = task.run(task=text.template_file,
name="Base Configuration",
template="base.j2",
path=f"templates/{task.host.platform}")
# Save the compiled configuration into a host variable
task.host["config"] = r.result
# Save the compiled configuration into a file
with open(f"configs/{task.host.hostname}", "w") as f:
f.write(r.result)
# Deploy that configuration to the device using NAPALM
task.run(task=networking.napalm_configure,
name="Loading Configuration on the device",
replace=False,
configuration=task.host["config"])
nr = InitNornir(config_file="config.yaml", dry_run=True) # set dry_run=False, cross your fingers and run again
# run tasks
result = nr.run(task=config_and_deploy)
print_result(result)
เปเบญเบปเบฒเปเบเปเบชเปเบเบฑเบเบเบฒเบฅเบฒเบกเบดเปเบเบต dry_run=เบเบดเบ เปเบเบเบฒเบเปเบฅเบตเปเบกเบเบปเปเบเบงเบฑเบเบเบธเปเบชเบฑเปเบ nr.
เบเบตเปเบเบตเปเบเบทเบเบฑเบเบเบฑเบเปเบ เบเบญเบเบฎเบฑเบเปเบเป เบเบฒเบเบเปเบฒเปเบเบตเบเบเบฒเบเบเบปเบเบชเบญเบเปเบเปเบเบทเบเบเบฐเบเบดเบเบฑเบเปเบเบเบฒเบเปเบเบทเปเบญเบกเบเปเปเบเบฑเบ router, เบเบฒเบเบเบฑเปเบเบเปเบฒเบเบฑเบเปเบเปเปเบซเบกเปเปเบเปเบเบทเบเบเบฐเบเบฝเบก, เปเบเบดเปเบเบซเบผเบฑเบเบเบฒเบเบเบฑเปเบเปเบเปเบฎเบฑเบเบเบฒเบเบเบงเบเบชเบญเบเปเบเบเบญเบธเบเบฐเบเบญเบ (เปเบเปเบเบตเปเบเปเปเปเบเปเบเบญเบ; เบกเบฑเบเบเบถเปเบเบเบฑเบเบเบฒเบเบชเบฐเบซเบเบฑเบเบชเบฐเบซเบเบนเบเบญเบธเบเบฐเบเบญเบเปเบฅเบฐเบเบฒเบเบเบฑเบเบเบฑเปเบเบเบฐเบเบดเบเบฑเบเปเบเปเบงเบตเปเบ NAPALM) , เปเบเปเบเบฒเบเบเบฑเปเบเบเปเบฒเปเบซเบกเปเบเปเปเปเบเปเบเบทเบเบเปเบฒเปเบเปเปเบเบเบเบปเบ. เบชเปเบฒเบฅเบฑเบเบเบฒเบเบเปเบฒเปเบเปเบเบฒเบเบเปเปเบชเบนเป, เบเปเบฒเบเบเปเบญเบเปเบญเบปเบฒเบเบฒเบฅเบฒเบกเบดเปเบเบตเบญเบญเบ dry_run เบซเบผเบทเบเปเบฝเบเบกเบนเบเบเปเบฒเบเบญเบเบกเบฑเบเปเบเบฑเบ เบเบตเปเบเปเปเบเบทเบเบเปเบญเบ.
เปเบกเบทเปเบญเบชเบฐเบเบฃเบดเบเบเบทเบเบเบฐเบเบดเบเบฑเบ, Nornir เบชเบปเปเบเบเบฑเบเบเบถเบเบฅเบฒเบเบฅเบฐเบญเบฝเบเปเบซเปเบเบฑเบ console.
เบเปเบฒเบเบฅเบธเปเบกเบเบตเป spoiler เปเบกเปเบเบเบปเบเบเบฐเบฅเบดเบเบเบญเบ combat run เบชเบธเบเบชเบญเบ routers เบเบฒเบเบเบปเบเบชเบญเบ:
config_and_deploy***************************************************************
* cisco-test ** changed : True *******************************************
vvvv config_and_deploy ** changed : True vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv INFO
---- Base Configuration ** changed : True ------------------------------------- INFO
class-map match-all VIDEO_SURV
match access-group 111
policy-map VIDEO_SURV
class VIDEO_SURV
police 1500000 conform-action transmit exceed-action drop
interface GigabitEthernet0/1/1
description VIDEOSURV
ip address 10.10.222.254 255.255.255.0
service-policy input VIDEO_SURV
router bgp 65001
network 10.10.222.0 mask 255.255.255.0
access-list 11 permit 10.10.222.0 0.0.0.255
access-list 111 permit ip 10.10.222.0 0.0.0.255 any
---- Loading Configuration on the device ** changed : True --------------------- INFO
+class-map match-all VIDEO_SURV
+ match access-group 111
+policy-map VIDEO_SURV
+ class VIDEO_SURV
+interface GigabitEthernet0/1/1
+ description VIDEOSURV
+ ip address 10.10.222.254 255.255.255.0
+ service-policy input VIDEO_SURV
+router bgp 65001
+ network 10.10.222.0 mask 255.255.255.0
+access-list 11 permit 10.10.222.0 0.0.0.255
+access-list 111 permit ip 10.10.222.0 0.0.0.255 any
^^^^ END config_and_deploy ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
* srx-test ** changed : True *******************************************
vvvv config_and_deploy ** changed : True vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv INFO
---- Base Configuration ** changed : True ------------------------------------- INFO
set interfaces fe-0/0/2 unit 0 description "Video surveillance"
set interfaces fe-0/0/2 unit 0 family inet filter input limit-in
set interfaces fe-0/0/2 unit 0 family inet address 10.10.111.254/24
set policy-options policy-statement export2bgp term 1 from route-filter 10.10.111.0/24 exact
set security zones security-zone WAN interfaces fe-0/0/2
set firewall policer policer-1m if-exceeding bandwidth-limit 1m
set firewall policer policer-1m if-exceeding burst-size-limit 187k
set firewall policer policer-1m then discard
set firewall policer policer-1.5m if-exceeding bandwidth-limit 1500000
set firewall policer policer-1.5m if-exceeding burst-size-limit 280k
set firewall policer policer-1.5m then discard
set firewall filter limit-in term 1 then policer policer-1.5m
set firewall filter limit-in term 1 then count limiter
---- Loading Configuration on the device ** changed : True --------------------- INFO
[edit interfaces]
+ fe-0/0/2 {
+ unit 0 {
+ description "Video surveillance";
+ family inet {
+ filter {
+ input limit-in;
+ }
+ address 10.10.111.254/24;
+ }
+ }
+ }
[edit]
+ policy-options {
+ policy-statement export2bgp {
+ term 1 {
+ from {
+ route-filter 10.10.111.0/24 exact;
+ }
+ }
+ }
+ }
[edit security zones]
security-zone test-vpn { ... }
+ security-zone WAN {
+ interfaces {
+ fe-0/0/2.0;
+ }
+ }
[edit]
+ firewall {
+ policer policer-1m {
+ if-exceeding {
+ bandwidth-limit 1m;
+ burst-size-limit 187k;
+ }
+ then discard;
+ }
+ policer policer-1.5m {
+ if-exceeding {
+ bandwidth-limit 1500000;
+ burst-size-limit 280k;
+ }
+ then discard;
+ }
+ filter limit-in {
+ term 1 {
+ then {
+ policer policer-1.5m;
+ count limiter;
+ }
+ }
+ }
+ }
^^^^ END config_and_deploy ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
เปเบเบทเปเบญเบเบฅเบฐเบซเบฑเบเบเปเบฒเบเบขเบนเปเปเบ ansible_vault
เปเบเบเบญเบเบเบปเปเบเบเบญเบเบเบปเบเบเบงเบฒเบกเบเปเบฒเบเบฐเปเบเบปเปเบฒเปเบเป overboard เบเบฝเบเปเบฅเบฑเบเบเปเบญเบ เบเบญเบเบฎเบฑเบเปเบเป, เปเบเปเบกเบฑเบเบเปเปเปเบกเปเบเบชเบดเปเบเบเบตเปเบเปเปเบเบตเบเบฑเบเบซเบกเบปเบ. เบเปเบญเบเบกเบฑเบเบเบงเบเปเบเบปเบฒเปเบเปเป vault เปเบเบฑเปเบเบงเปเบฒ, เปเบเบดเปเบเบเบทเบเบญเบญเบเปเบเบเบกเบฒเปเบเบทเปเบญเปเบเบทเปเบญเบเบเปเปเบกเบนเบเบเบตเปเบฅเบฐเบญเบฝเบเบญเปเบญเบเบญเบญเบเบเบฒเบเบชเบฒเบเบเบฒ. เปเบฅเบฐเบญเบฒเบเบเบฐเปเบเบฑเบเบเปเบฒเบเบงเบเบซเบผเบฒเบเปเบเปเบชเบฑเบเปเบเบเปเบซเบฑเบเบงเปเบฒเบเบงเบเปเบฎเบปเบฒเบกเบตเบเบฒเบเปเบเบปเปเบฒเบชเบนเปเบฅเบฐเบเบปเบ / เบฅเบฐเบซเบฑเบเบเปเบฒเบเบเบฑเบเบซเบกเบปเบเบชเปเบฒเบฅเบฑเบ routers เบชเบนเปเบฎเบปเบเบเบฑเบเบซเบกเบปเบ sparkling เปเบเบฎเบนเบเปเบเบเปเบเบตเบเบขเบนเปเปเบเปเบเบฅเป. gorups.yaml. เบกเบฑเบเบเปเปเบเบฒเบก, เปเบเปเบเบญเบ. เปเบซเปเบเบปเบเบเปเบญเบเบเปเปเบกเบนเบเบเบตเปเบเปเบงเบ vault.
เปเบซเปเบเบงเบเปเบฎเบปเบฒเปเบญเบเบเบฒเบฅเบฒเบกเบดเปเบเบตเบเบฒเบ groups.yaml เปเบ creds.yaml, เปเบฅเบฐเปเบเบปเปเบฒเบฅเบฐเบซเบฑเบเบกเบฑเบเบเปเบงเบ AES256 เบเปเบงเบเบฅเบฐเบซเบฑเบเบเปเบฒเบ 20 เบเบปเบงเปเบฅเบ:
$ cd inventory
$ cat creds.yaml
---
cisco:
username: admin1
password: cisco1
juniper:
username: admin2
password: juniper2
$ pwgen 20 -N 1 > vault.passwd
ansible-vault encrypt creds.yaml --vault-password-file vault.passwd
Encryption successful
$ cat creds.yaml
$ANSIBLE_VAULT;1.1;AES256
39656463353437333337356361633737383464383231366233386636333965306662323534626131
3964396534396333363939373539393662623164373539620a346565373439646436356438653965
39643266333639356564663961303535353364383163633232366138643132313530346661316533
6236306435613132610a656163653065633866626639613537326233653765353661613337393839
62376662303061353963383330323164633162386336643832376263343634356230613562643533
30363436343465306638653932366166306562393061323636636163373164613630643965636361
34343936323066393763323633336366366566393236613737326530346234393735306261363239
35663430623934323632616161636330353134393435396632663530373932383532316161353963
31393434653165613432326636616636383665316465623036376631313162646435
เบกเบฑเบเบเปเบฒเบเบเบฒเบเบเบฑเปเบ. เบกเบฑเบเบเบฑเบเบเบปเบเบชเบญเบเบเบญเบเบเบงเบเปเบฎเบปเบฒ เบเปเปเบเบต-script เปเบเบทเปเบญเบเบถเบเบเปเปเบกเบนเบ เปเบฅเบฐเบเบณเปเบเปเบเปเปเบกเบนเบเบเบตเป.
เปเบเบทเปเบญเปเบฎเบฑเบเบชเบดเปเบเบเบตเป, เปเบเบชเบฐเบเบดเบเบเบญเบเบเบงเบเปเบฎเบปเบฒเบซเบผเบฑเบเบเบฒเบเปเบชเบฑเปเบเบเบฒเบเปเบฅเบตเปเบกเบเบปเปเบ nr = InitNornir(config_file=โฆ เปเบเบตเปเบกเบฅเบฐเบซเบฑเบเบเปเปเปเบเบเบตเป:
...
nr = InitNornir(config_file="config.yaml", dry_run=True) # set dry_run=False, cross your fingers and run again
# enrich Inventory with the encrypted vault data
from ansible_vault import Vault
vault_password_file="inventory/vault.passwd"
vault_file="inventory/creds.yaml"
with open(vault_password_file, "r") as fp:
password = fp.readline().strip()
vault = Vault(password)
vaultdata = vault.load(open(vault_file).read())
for a in nr.inventory.hosts.keys():
item = nr.inventory.hosts[a]
item.username = vaultdata[item.groups[0]]['username']
item.password = vaultdata[item.groups[0]]['password']
#print("hostname={}, username={}, password={}n".format(item.hostname, item.username, item.password))
# run tasks
...
เปเบเปเบเบญเบ, vault.passwd เบเปเปเบเบงเบเบเบฑเปเบเบขเบนเปเปเบเปเบเบฑเบ creds.yaml เปเบเบเบปเบงเบขเปเบฒเบเบเบญเบเบเปเบญเบ. เปเบเปเบกเบฑเบเบเปเปเปเบเบฑเบเบซเบเบฑเบเบชเปเบฒเบฅเบฑเบเบเบฒเบเบซเบผเบตเปเบ.
เบเบฑเปเบเปเบกเปเบเบเบฑเบเบซเบกเบปเบเบชเปเบฒเบฅเบฑเบเปเบเบเบฑเบเบเบธเบเบฑเบ. เบกเบตเบเบปเบเบเบงเบฒเบกเบชเบญเบเบชเบฒเบกเบขเปเบฒเบเบเปเบฝเบงเบเบฑเบ Cisco + Zabbix เบเบฐเบกเบฒเปเบเบดเบ, เปเบเปเบเบตเปเบเปเปเปเบกเปเบเปเบฅเบทเปเบญเบเบญเบฑเบเบเบฐเปเบเบกเบฑเบ. เปเบฅเบฐเปเบเบญเบฐเบเบฒเบเบปเบเบญเบฑเบเปเบเปเบเบตเปเบเปเบฒเบเบฐเปเบเบปเปเบฒเบงเบฒเบเปเบเบเบเบตเปเบเบฐเบเบฝเบเบเปเบฝเบงเบเบฑเบ RESTCONF เปเบ Cisco.
เปเบซเบผเปเบเบเปเปเบกเบนเบ: www.habr.com