เฮ้ ฮับ!
ล่าสุดมีบทความปรากฏขึ้นที่นี่
นี่ไม่ใช่จักรยานคันแรกสำหรับงานดังกล่าว ตัวเลือกแรกถูกนำมาใช้เมื่อหลายปีก่อน เบิ้ล เวอร์ชัน 1.x.x จักรยานไม่ค่อยได้ใช้งานจึงเกิดสนิมอยู่ตลอดเวลา ในแง่ที่ว่างานนั้นไม่ได้เกิดขึ้นบ่อยเท่าที่เวอร์ชันได้รับการอัปเดต เบิ้ล. และทุกครั้งที่ต้องขับ โซ่หลุด หรือล้อหลุด อย่างไรก็ตาม ส่วนแรก การสร้างการกำหนดค่า โชคดีเสมอ จินจา2 เครื่องยนต์มีมายาวนาน แต่ส่วนที่สอง - การเปิดตัวการกำหนดค่า - มักจะนำมาซึ่งความประหลาดใจ และเนื่องจากฉันต้องเปิดใช้การกำหนดค่าจากระยะไกลกับอุปกรณ์ครึ่งร้อยเครื่อง ซึ่งบางเครื่องอยู่ห่างออกไปหลายพันกิโลเมตร การใช้เครื่องมือนี้จึงค่อนข้างน่าเบื่อ
ในที่นี้ฉันต้องยอมรับว่าความไม่แน่นอนของฉันน่าจะเกิดจากการขาดความคุ้นเคย เบิ้ลมากกว่าในข้อบกพร่องของมัน และนี่คือจุดสำคัญ เบิ้ล เป็นพื้นที่ความรู้ที่แยกจากกันโดยสิ้นเชิงโดยมี DSL (Domain Individual Language) ของตัวเองซึ่งจะต้องได้รับการดูแลในระดับความมั่นใจ นั่นแหละ วินาทีนั้น. เบิ้ล มีการพัฒนาค่อนข้างเร็ว และไม่ได้คำนึงถึงความเข้ากันได้แบบย้อนหลังเป็นพิเศษ แต่ก็ไม่ได้เพิ่มความมั่นใจ
ดังนั้นเมื่อไม่นานมานี้จึงมีการนำจักรยานรุ่นที่สองมาใช้ ในครั้งนี้ หลามหรือค่อนข้างจะอยู่บนกรอบที่เขียนมา หลาม และสำหรับ หลาม ที่เรียกว่า
ดังนั้น - นอร์เนียร์ เป็นไมโครเฟรมเวิร์กที่เขียนไว้ หลาม และสำหรับ หลาม และออกแบบมาเพื่อระบบอัตโนมัติ เช่นเดียวกับในกรณีที่มี เบิ้ลในการแก้ปัญหาที่นี่ จำเป็นต้องมีการเตรียมข้อมูลที่มีความสามารถ เช่น รายการโฮสต์และพารามิเตอร์ แต่สคริปต์ไม่ได้เขียนใน DSL แยกต่างหาก แต่ในแบบเดียวกันไม่เก่ามาก แต่ p[i|i]ton ดีมาก
ลองดูว่ามันคืออะไรโดยใช้ตัวอย่างสดต่อไปนี้
ฉันมีเครือข่ายสาขาที่มีสำนักงานหลายสิบแห่งทั่วประเทศ สำนักงานแต่ละแห่งมีเราเตอร์ WAN ที่จะยุติช่องทางการสื่อสารหลายช่องทางจากผู้ให้บริการที่แตกต่างกัน โปรโตคอลการกำหนดเส้นทางคือ BGP เราเตอร์ WAN มีสองประเภท: Cisco ISG หรือ Juniper SRX
ตอนนี้งาน: คุณต้องกำหนดค่าเครือข่ายย่อยเฉพาะสำหรับการเฝ้าระวังวิดีโอบนพอร์ตแยกต่างหากบนเราเตอร์ WAN ทั้งหมดของเครือข่ายสาขา - โฆษณาเครือข่ายย่อยนี้ใน BGP - กำหนดค่าขีดจำกัดความเร็วของพอร์ตเฉพาะ
ขั้นแรก เราต้องเตรียมเทมเพลตสองสามรายการ โดยขึ้นอยู่กับการกำหนดค่าที่จะสร้างแยกต่างหากสำหรับ Cisco และ Juniper นอกจากนี้ยังจำเป็นต้องเตรียมข้อมูลสำหรับแต่ละจุดและพารามิเตอร์การเชื่อมต่อ เช่น รวบรวมสินค้าคงคลังเดียวกัน
เทมเพลตที่พร้อมสำหรับ 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
เทมเพลตสำหรับจูนิเปอร์:
$ 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
แน่นอนว่าเทมเพลตไม่ได้หลุดลอยไป สิ่งเหล่านี้มีความแตกต่างกันโดยพื้นฐานระหว่างการกำหนดค่าการทำงานที่เกิดขึ้นและหลังจากแก้ไขปัญหาบนเราเตอร์เฉพาะสองตัวที่มีรุ่นต่างกัน
จากเทมเพลตของเรา เราพบว่าในการแก้ปัญหา เราต้องการเพียงพารามิเตอร์สองตัวสำหรับ Juniper และพารามิเตอร์ 3 ตัวสำหรับ Cisco พวกเขาอยู่ที่นี่:
- ถ้าชื่อ
- ไอพีต่อท้าย
- อัศ
ตอนนี้เราต้องตั้งค่าพารามิเตอร์เหล่านี้ให้กับแต่ละอุปกรณ์ เช่น ทำสิ่งเดียวกัน สินค้าคงคลัง.
สำหรับ สินค้าคงคลัง เราจะปฏิบัติตามเอกสารอย่างเคร่งครัด
นั่นคือเรามาสร้างโครงกระดูกไฟล์เดียวกันกันดีกว่า:
.
├── 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
และนี่คือ groups.yaml:
---
cisco:
platform: ios
username: admin1
password: cisco1
juniper:
platform: junos
username: admin2
password: juniper2
นี่คือสิ่งที่เกิดขึ้น สินค้าคงคลัง สำหรับงานของเรา ในระหว่างการเริ่มต้น พารามิเตอร์จากไฟล์สินค้าคงคลังจะถูกแมปกับโมเดลออบเจ็กต์ องค์ประกอบสินค้าคงคลัง.
ด้านล่างสปอยเลอร์คือไดอะแกรมของโมเดล 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"
}
}
}
}
}
โมเดลนี้อาจดูสับสนเล็กน้อยโดยเฉพาะในช่วงแรกๆ เพื่อที่จะเข้าใจ โหมดโต้ตอบจึงเข้ามา หลาม.
$ 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.
ที่นี่เช่นเดียวกับใน เบิ้ล มีการดำเนินการทดสอบโดยทำการเชื่อมต่อกับเราเตอร์ มีการเตรียมการกำหนดค่าที่แก้ไขใหม่ ซึ่งได้รับการตรวจสอบโดยอุปกรณ์ (แต่ก็ไม่แน่ใจ ขึ้นอยู่กับการรองรับอุปกรณ์และการใช้งานไดรเวอร์ใน NAPALM) แต่ไม่ได้ใช้การกำหนดค่าใหม่โดยตรง สำหรับการใช้งานการต่อสู้ คุณต้องลบพารามิเตอร์ออก ดราย_รัน หรือเปลี่ยนค่าเป็น เท็จ.
เมื่อสคริปต์ถูกเรียกใช้งาน Nornir จะส่งออกบันทึกโดยละเอียดไปยังคอนโซล
ด้านล่างสปอยเลอร์คือผลลัพธ์ของการต่อสู้บนเราเตอร์ทดสอบสองตัว:
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
ในตอนต้นของบทความฉันพูดมากเกินไปเล็กน้อย เบิ้ลแต่ก็ไม่ได้แย่ขนาดนั้น ฉันชอบพวกเขาจริงๆ หกคะเมน เช่นซึ่งออกแบบมาเพื่อซ่อนข้อมูลที่ละเอียดอ่อนให้พ้นสายตา และหลายคนอาจสังเกตเห็นว่าเรามีข้อมูลเข้าสู่ระบบ/รหัสผ่านสำหรับเราเตอร์การต่อสู้ทั้งหมดในรูปแบบเปิดในไฟล์ gorups.yaml. มันไม่สวยแน่นอน มาปกป้องข้อมูลนี้ด้วย หกคะเมน.
มาโอนพารามิเตอร์จาก 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
ที่มา: will.com