ஹே ஹப்ர்!
சமீபத்தில் ஒரு கட்டுரை இங்கு வந்தது
இதுபோன்ற பணிக்கு இது முதல் பைக் அல்ல. முதல் விருப்பம் பல ஆண்டுகளுக்கு முன்பு செயல்படுத்தப்பட்டது ansible பதிப்பு 1.x.x. சைக்கிள் அரிதாகவே பயன்படுத்தப்பட்டது, எனவே தொடர்ந்து துருப்பிடித்தது. பதிப்புகள் புதுப்பிக்கப்படும்போது பணி தானாகவே எழுவதில்லை என்ற பொருளில் ansible. ஒவ்வொரு முறையும் நீங்கள் வாகனம் ஓட்ட வேண்டியிருக்கும் போது, சங்கிலி விழுகிறது அல்லது சக்கரம் விழுகிறது. இருப்பினும், முதல் பகுதி, கட்டமைப்புகளை உருவாக்குவது, எப்போதும் மிகவும் தெளிவாக, அதிர்ஷ்டவசமாக வேலை செய்கிறது ஜின்ஜா2 இயந்திரம் நீண்ட காலமாக நிறுவப்பட்டுள்ளது. ஆனால் இரண்டாவது பகுதி - கட்டமைப்புகளை உருட்டுதல் - பொதுவாக ஆச்சரியங்களைக் கொண்டு வந்தது. மேலும் நான் கட்டமைப்பை தொலைவிலிருந்து அரை நூறு சாதனங்களுக்கு உருட்ட வேண்டும், அவற்றில் சில ஆயிரக்கணக்கான கிலோமீட்டர் தொலைவில் அமைந்துள்ளன, இந்த கருவியைப் பயன்படுத்துவது கொஞ்சம் சலிப்பாக இருந்தது.
எனது நிச்சயமற்ற தன்மை பெரும்பாலும் எனது பரிச்சயமின்மையில் உள்ளது என்பதை இங்கே நான் ஒப்புக் கொள்ள வேண்டும் ansibleஅதன் குறைபாடுகளை விட. மேலும், இது ஒரு முக்கியமான புள்ளி. ansible முற்றிலும் தனியான, அதன் சொந்த DSL (டொமைன் ஸ்பெசிஃபிக் லாங்குவேஜ்) உடன் அதன் சொந்த அறிவுப் பகுதி, இது நம்பிக்கையான அளவில் பராமரிக்கப்பட வேண்டும். சரி, அந்த தருணம் ansible இது மிக விரைவாக வளர்ந்து வருகிறது, மேலும் பின்தங்கிய பொருந்தக்கூடிய தன்மைக்கு சிறப்பு கவனம் செலுத்தாமல், அது நம்பிக்கையை சேர்க்காது.
எனவே, மிக நீண்ட காலத்திற்கு முன்பு சைக்கிளின் இரண்டாவது பதிப்பு செயல்படுத்தப்பட்டது. இந்த முறை மலைப்பாம்பு, அல்லது எழுதப்பட்ட கட்டமைப்பில் மலைப்பாம்பு மற்றும் மலைப்பாம்பு என்ற தலைப்பில்
அதனால் - நார்னிர் மைக்ரோஃப்ரேம்வொர்க் எழுதப்பட்டுள்ளது மலைப்பாம்பு மற்றும் மலைப்பாம்பு மற்றும் ஆட்டோமேஷனுக்காக வடிவமைக்கப்பட்டது. வழக்கில் அதே ansible, இங்குள்ள சிக்கல்களைத் தீர்க்க, திறமையான தரவுத் தயாரிப்பு தேவை, அதாவது. ஹோஸ்ட்களின் பட்டியல் மற்றும் அவற்றின் அளவுருக்கள், ஆனால் ஸ்கிரிப்டுகள் ஒரு தனி DSL இல் எழுதப்படவில்லை, ஆனால் அதே மிகவும் பழையதாக இல்லை, ஆனால் மிகவும் நல்ல p[i|i]ton.
பின்வரும் நேரடி உதாரணத்தைப் பயன்படுத்தி அது என்ன என்பதைப் பார்ப்போம்.
நான் நாடு முழுவதும் பல டஜன் அலுவலகங்களைக் கொண்ட கிளை நெட்வொர்க்கைக் கொண்டுள்ளேன். ஒவ்வொரு அலுவலகத்திலும் ஒரு WAN திசைவி உள்ளது, இது வெவ்வேறு ஆபரேட்டர்களிடமிருந்து பல தொடர்பு சேனல்களை நிறுத்துகிறது. ரூட்டிங் நெறிமுறை BGP ஆகும். WAN திசைவிகள் இரண்டு வகைகளில் வருகின்றன: சிஸ்கோ ஐஎஸ்ஜி அல்லது ஜூனிபர் எஸ்ஆர்எக்ஸ்.
இப்போது பணி: கிளை நெட்வொர்க்கின் அனைத்து WAN ரவுட்டர்களிலும் ஒரு தனி போர்ட்டில் வீடியோ கண்காணிப்புக்கான பிரத்யேக சப்நெட்டை உள்ளமைக்க வேண்டும் - இந்த சப்நெட்டை BGP இல் விளம்பரப்படுத்தவும் - அர்ப்பணிக்கப்பட்ட போர்ட்டின் வேக வரம்பை உள்ளமைக்கவும்.
முதலில், நாம் இரண்டு வார்ப்புருக்களை தயார் செய்ய வேண்டும், அதன் அடிப்படையில் சிஸ்கோ மற்றும் ஜூனிபருக்கு தனித்தனியாக கட்டமைப்புகள் உருவாக்கப்படும். ஒவ்வொரு புள்ளி மற்றும் இணைப்பு அளவுருக்களுக்கும் தரவைத் தயாரிப்பதும் அவசியம், அதாவது. அதே சரக்குகளை சேகரிக்கவும்
சிஸ்கோவிற்கான தயார் டெம்ப்ளேட்:
$ 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
டெம்ப்ளேட்கள், நிச்சயமாக, மெல்லிய காற்றில் இருந்து வெளியே வரவில்லை. வெவ்வேறு மாதிரிகளின் இரண்டு குறிப்பிட்ட ரவுட்டர்களில் பணியைத் தீர்ப்பதற்குப் பிறகு வேலை செய்யும் உள்ளமைவுகளுக்கு இடையே உள்ள வேறுபாடுகள் இவை.
சிக்கலைத் தீர்க்க, ஜூனிபருக்கு இரண்டு அளவுருக்கள் மற்றும் சிஸ்கோவிற்கு 3 அளவுருக்கள் மட்டுமே தேவை என்பதை எங்கள் டெம்ப்ளேட்களில் இருந்து பார்க்கிறோம். இங்கே அவர்கள்:
- என்றால் பெயர்
- ipsuffix
- asn
இப்போது ஒவ்வொரு சாதனத்திற்கும் இந்த அளவுருக்களை அமைக்க வேண்டும், அதாவது. அதையே செய் சரக்கு.
செய்ய சரக்கு நாங்கள் ஆவணங்களை கண்டிப்பாக பின்பற்றுவோம்
அதாவது, அதே கோப்பு எலும்புக்கூட்டை உருவாக்குவோம்:
.
├── 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, குழு (என் விஷயத்தில் இவை உள்நுழைவுகள்/கடவுச்சொற்கள்) இல் குழுக்கள்.யாம்ல்மற்றும் உள்ளே defaults.yaml நாங்கள் எதையும் குறிப்பிட மாட்டோம், ஆனால் நீங்கள் அங்கு மூன்று கழித்தல்களை உள்ளிட வேண்டும் - அது இருப்பதைக் குறிக்கிறது யாழ் கோப்பு காலியாக உள்ளது.
இது 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)
அளவுருவுக்கு கவனம் செலுத்துங்கள் உலர்_ரன்=உண்மை வரி பொருள் துவக்கத்தில் nr.
இங்கே உள்ளதைப் போலவே ansible ஒரு சோதனை ஓட்டம் செயல்படுத்தப்பட்டது, அதில் ரூட்டருக்கான இணைப்பு உருவாக்கப்பட்டு, ஒரு புதிய மாற்றியமைக்கப்பட்ட உள்ளமைவு தயாரிக்கப்பட்டது, அது சாதனத்தால் சரிபார்க்கப்படுகிறது (ஆனால் இது உறுதியாகத் தெரியவில்லை; இது சாதன ஆதரவு மற்றும் NAPALM இல் இயக்கி செயல்படுத்தலைப் பொறுத்தது) , ஆனால் புதிய கட்டமைப்பு நேரடியாகப் பயன்படுத்தப்படவில்லை. போர் பயன்பாட்டிற்கு, நீங்கள் அளவுருவை அகற்ற வேண்டும் உலர்ந்த ஓட்டம் அல்லது அதன் மதிப்பை மாற்றவும் தவறான.
ஸ்கிரிப்ட் செயல்படுத்தப்படும் போது, நார்னிர் கன்சோலில் விரிவான பதிவுகளை வெளியிடுகிறது.
ஸ்பாய்லருக்கு கீழே இரண்டு சோதனை ரவுட்டர்களில் போர் ஓட்டத்தின் வெளியீடு உள்ளது:
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 இல் மறைத்தல்
கட்டுரையின் ஆரம்பத்தில் நான் கொஞ்சம் அதிகமாகவே சென்றேன் ansible, ஆனால் அது அவ்வளவு மோசமாக இல்லை. எனக்கு அவர்களை மிகவும் பிடிக்கும் பெட்டகத்தை போன்ற, இது முக்கியமான தகவல்களை பார்வைக்கு வெளியே மறைக்க வடிவமைக்கப்பட்டுள்ளது. ஒரு கோப்பில் திறந்த வடிவத்தில் பிரகாசிக்கும் அனைத்து போர் திசைவிகளுக்கான அனைத்து உள்நுழைவுகள் / கடவுச்சொற்கள் எங்களிடம் இருப்பதை பலர் கவனித்திருக்கலாம். gorups.yaml. இது அழகாக இல்லை, நிச்சயமாக. இந்தத் தரவைக் கொண்டு பாதுகாப்போம் பெட்டகத்தை.
Group.yaml இலிருந்து creds.yaml க்கு அளவுருக்களை மாற்றி, 256 இலக்க கடவுச்சொல்லுடன் AES20 உடன் குறியாக்கம் செய்வோம்:
$ 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
இது மிகவும் எளிமையானது. இது நமக்கு கற்பிக்க உள்ளது நார்னிர்இந்தத் தரவை மீட்டெடுக்கவும் பயன்படுத்தவும் ஸ்கிரிப்ட்.
இதைச் செய்ய, துவக்க வரிக்குப் பிறகு எங்கள் ஸ்கிரிப்ட்டில் 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 பற்றி எழுத திட்டமிட்டுள்ளேன்.
ஆதாரம்: www.habr.com