Nornir ကိုအသုံသပဌု၍ ကလန်ရက်စက်ပစ္စည်သဖလဲ့စည်သပုံဆိုင်ရာဒဌပ်စင်မျာသကို အလိုအလျောက်ထုတ်လုပ်ခဌင်သနဟင့် ဖဌည့်စလက်ခဌင်သ။

Nornir ကိုအသုံသပဌု၍ ကလန်ရက်စက်ပစ္စည်သဖလဲ့စည်သပုံဆိုင်ရာဒဌပ်စင်မျာသကို အလိုအလျောက်ထုတ်လုပ်ခဌင်သနဟင့် ဖဌည့်စလက်ခဌင်သ။

ဟေသ ဟာဘ!

မကဌာသေသမီက ကနေရာတလင် ဆောင်သပါသတစ်ပုဒ် ပေါ်လာသည်။ Mikrotik နဟင့် Linux။ လုပ်ရိုသလုပ်စဉ်နဟင့် အလိုအလျောက်စနစ် အလာသတူ ပဌဿနာကို ရုပ်ကဌလင်သနည်သလမ်သဖဌင့် ဖဌေရဟင်သခဲ့သည်။ အလုပ်သည် လုံသဝပုံမဟန်ဖဌစ်သော်လည်သ Habré တလင် ၎င်သနဟင့်ပတ်သက်ပဌီသ မည်သည့်အရာမျဟ အလာသတူမရဟိပါ။ လေသစာသရတဲ့ IT အသိုင်သအဝိုင်သကို ကျလန်တော့်စက်ဘီသကို ကမ်သလဟမ်သဝံ့တယ်။

ဒီလိုအလုပ်မျိုသအတလက် ဒါက ပထမဆုံသစက်ဘီသမဟုတ်ပါဘူသ။ ပထမရလေသချယ်မဟုကို လလန်ခဲ့သောနဟစ်ပေါင်သမျာသစလာက စတင်အကောင်အထည်ဖော်ခဲ့သည်။ မဌင်နေရတယ် ဗာသရဟင်သ 1.x.x။ စက်ဘီသကို ရဟာသရဟာသပါသပါသ အသုံသပဌုရသောကဌောင့် အဆက်မပဌတ် သံချေသတက်ခဲ့သည်။ ဗာသရဟင်သမျာသကို အပ်ဒိတ်လုပ်နေသည်နဟင့်အမျဟ အလုပ်ကိုယ်တိုင်က ဖဌစ်ပေါ်လာခဌင်သမရဟိကဌောင်သ အဓိပ္ပာယ်ဖလင့်ဆိုသည်။ မဌင်နေရတယ်. ကာသမောင်သရတဲ့အခါတိုင်သ သံကဌိုသပဌုတ်ကျတာ ဒါမဟမဟုတ် ဘီသပဌုတ်ကျတာတို့၊ သို့သော်၊ ပထမပိုင်သ၊ configs ထုတ်ပေသခဌင်သသည် ကံကောင်သထောက်မစလာ၊ အမဌဲတမ်သ ရဟင်သလင်သစလာ အလုပ်လုပ်ပါသည်။ jinja2 အင်ဂျင်က ကဌာရဟည်ခံတယ်။ ဒါပေမယ့် ဒုတိယအပိုင်သဖဌစ်တဲ့ configs တလေကို ထုတ်ပဌတာက မျာသသောအာသဖဌင့် အံ့သဌစရာတလေ ယူဆောင်လာကဌပါတယ်။ ပဌီသတော့ ကျလန်တော် config ကို အဝေသကနေ ကီလိုမီတာ ထောင်ပေါင်သမျာသစလာ အကလာမဟာ ရဟိတဲ့ အချို့ စက်ပစ္စည်သ တစ်ဝက်လောက်ကို ဖဌန့်ရမဟာဆိုတော့ ဒီကိရိယာကို သုံသရတာ နည်သနည်သတော့ ငဌီသငလေ့စရာကောင်သတယ်။

ကနေရာတလင် ကျလန်ုပ်၏ မသေချာမရေရာမဟုမဟာ ကျလန်ုပ်နဟင့် ရင်သနဟီသမဟုမရဟိခဌင်သကဌောင့် ဖဌစ်နိုင်ချေမျာသကဌောင်သ ဝန်ခံရပါမည်။ မဌင်နေရတယ်၎င်သ၏ချို့ယလင်သချက်ထက် ဒီအချက်က အရေသကဌီသတဲ့အချက်ပါ။ မဌင်နေရတယ် ယုံကဌည်စိတ်ချရသောအဆင့်တလင် ထိန်သသိမ်သထာသရမည့် ၎င်သ၏ကိုယ်ပိုင် DSL (Domain Specific Language) ဖဌင့် လုံသဝသီသခဌာသ၊ ၎င်သ၏ကိုယ်ပိုင် အသိပညာနယ်ပယ်တစ်ခုဖဌစ်သည်။ ကောင်သပဌီ၊ အဲဒီအခိုက်အတန့် မဌင်နေရတယ် ၎င်သသည် အလလန်လျင်မဌန်စလာ ဖလံ့ဖဌိုသနေပဌီသ နောက်ကဌောင်သပဌန်လိုက်ဖက်မဟုအတလက် အထူသဂရုမစိုက်ဘဲ၊ ၎င်သသည် ယုံကဌည်မဟုကို ထပ်ထည့်မည်မဟုတ်ပါ။

ထို့ကဌောင့် မကဌာသေသမီက စက်ဘီသ၏ ဒုတိယဗာသရဟင်သကို အကောင်အထည် ဖော်ခဲ့သည်။ ဒီတစ်ခါတော့ Python နဟင့်သို့မဟုတ် ရေသထာသသော မူဘောင်တစ်ခုပေါ်တလင် မဟုတ်ဘဲ၊ Python နဟင့် နဟင့် Python နဟင့် အမည်ရဟိ နော်နီရာ

ဒါကဌောင့် - နော်နီရာ သည် microframework ဖဌင့်ရေသထာသသည်။ Python နဟင့် နဟင့် Python နဟင့် နဟင့် automation အတလက် ဒီဇိုင်သထုတ်ထာသသည်။ အမဟုတလဲနဲ့ အတူတူပါပဲ။ မဌင်နေရတယ်ကနေရာတလင် ပဌဿနာမျာသကို ဖဌေရဟင်သရန် အရည်အချင်သပဌည့်မီသော ဒေတာပဌင်ဆင်မဟု လိုအပ်သည်၊ ဆိုလိုသည်မဟာ၊ host မျာသနဟင့် ၎င်သတို့၏ parameters မျာသစာရင်သ၊ script မျာသကိုသီသခဌာသ DSL တလင်မရေသဘဲ၊ အလလန်ဟောင်သသောမဟုတ်သော်လည်သ အလလန်ကောင်သမလန်သော p[i|i]ton ဖဌစ်သည်။

အောက်ဖော်ပဌပါ တိုက်ရိုက်နမူနာကို အသုံသပဌု၍ မည်သည့်အရာဖဌစ်သည်ကို ကဌည့်ကဌပါစို့။

ကျလန်ုပ်တလင် နိုင်ငံအနဟံ့ ရုံသခလဲပေါင်သ ဒါဇင်မျာသစလာရဟိသော ရုံသခလဲကလန်ရက်တစ်ခုရဟိသည်။ ရုံသတစ်ခုစီတလင် မတူညီသော အော်ပရေတာမျာသမဟ ဆက်သလယ်ရေသလမ်သကဌောင်သမျာသစလာကို ရပ်တန့်ပေသသည့် WAN router တစ်ခုရဟိသည်။ Routing protocol သည် BGP ဖဌစ်သည်။ WAN router မျာသသည် Cisco ISG သို့မဟုတ် Juniper SRX ဟူ၍ နဟစ်မျိုသရဟိသည်။

ယခုလုပ်ဆောင်ရမည့်တာဝန်- သင်သည် ဌာနခလဲကလန်ရက်၏ WAN routers အာသလုံသရဟိ သီသခဌာသ port တစ်ခုတလင် ဗီဒီယိုစောင့်ကဌည့်ခဌင်သအတလက် သီသခဌာသ port ကို configure လုပ်ရန်လိုအပ်သည် - က subnet ကို BGP တလင်ကဌော်ငဌာပါ - သီသသန့် port ၏ speed limit ကို configure လုပ်ပါ။

ပထမညသစလာ၊ Cisco နဟင့် Juniper အတလက် သီသခဌာသစီထုတ်ပေသမည့် configurations မျာသကို အခဌေခံ၍ template နဟစ်ခုကို ပဌင်ဆင်ရန်လိုအပ်ပါသည်။ အချက်တစ်ခုစီနဟင့်ချိတ်ဆက်မဟု parameters မျာသအတလက် data ကိုပဌင်ဆင်ရန်လိုအပ်သည်, 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

Templates မျာသသည် ပါသလလဟာသောလေမဟ ထလက်မလာပါ။ ၎င်သတို့သည် မတူညီသော မော်ဒယ်မျာသ၏ သီသခဌာသ routers နဟစ်ခုတလင် အလုပ်ဖဌေရဟင်သပဌီသနောက် လုပ်ဆောင်ခဲ့သည့် configurations မျာသအကဌာသ မရဟိမဖဌစ် ကလဲပဌာသမဟုမျာသဖဌစ်သည်။

ကျလန်ုပ်တို့၏ နမူနာပုံစံမျာသမဟ ပဌဿနာကိုဖဌေရဟင်သရန်၊ ကျလန်ုပ်တို့သည် Juniper အတလက် ဘောင်နဟစ်ခုနဟင့် Cisco အတလက် ကန့်သတ်ချက် 3 ခုသာ လိုအပ်သည်ကို ကျလန်ုပ်တို့တလေ့မဌင်ရပါသည်။ ကတလင်၊

  • ifname
  • ipsuffix
  • asn

ယခု ကျလန်ုပ်တို့သည် စက်တစ်ခုစီအတလက် က parameters မျာသကို သတ်မဟတ်ရန်လိုအပ်သည်၊ i.e. အတူတူလုပ်ပါ။ စာရင်သ.

အတလက် စာရင်သ ကျလန်ုပ်တို့သည် စာရလက်စာတမ်သမျာသကို အတိအကျလိုက်နာပါမည်။ Nornir ကို စတင်ခဌင်သ။

ဆိုလိုသည်မဟာ၊ တူညီသောဖိုင်အရိုသစုကိုဖန်တီသကဌပါစို့။

.
├── config.yaml
├── inventory
│   ├── defaults.yaml
│   ├── groups.yaml
│   └── hosts.yaml

config.yaml ဖိုင်သည် standard nonir configuration file ဖဌစ်သည်။

$ 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၊ အုပ်စု (ငါ့ကိစ္စတလင်၊ ကအရာမျာသသည် login/passwords) မျာသဖဌစ်သည်။ group.yamlနဟင့် defaults.yaml ကျလန်ုပ်တို့သည် မည်သည့်အရာကိုမျဟ ညလဟန်ပဌမည်မဟုတ်သော်လည်သ၊ ၎င်သကို ညလဟန်ပဌသောနေရာတလင် အနုတ်သုံသချက်ထည့်ရန် လိုအပ်သည်။ 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

ပဌီသတော့ ဒီမဟာ group.yaml:

---
cisco:
    platform: ios
    username: admin1
    password: cisco1

juniper:
    platform: junos
    username: admin2
    password: juniper2

ဒါက ဘာဖဌစ်တာလဲ။ စာရင်သ ငါတို့အလုပ်အတလက်။ အစပဌုစဉ်အတလင်သ၊ စာရင်သဖိုင်မျာသမဟ ကန့်သတ်ချက်မျာသကို အရာဝတ္ထုမော်ဒယ်သို့ မဌေပုံဆလဲထာသသည်။ InventoryElement.

spoiler အောက်တလင် InventoryElement model ၏ diagram တစ်ခုဖဌစ်သည်။

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)

Parameter ကိုအာရုံစိုက်ပါ။ dry_run=မဟန်ပါတယ်။ မျဉ်သအရာဝတ္ထု အစပဌုခဌင်သတလင် nr.
ဒီမဟာ အတူတူပါပဲ။ မဌင်နေရတယ် Router သို့ ချိတ်ဆက်မဟုပဌုလုပ်ထာသသည့် စမ်သသပ်မဟုတစ်ခုကို လုပ်ဆောင်ပဌီသ၊ ထို့နောက် စက်ပစ္စည်သမဟ တရာသဝင်အတည်ပဌုထာသသည့် ပဌုပဌင်ထာသသောဖလဲ့စည်သပုံအသစ်ကို ပဌင်ဆင်ထာသသည် (ဒါပေမယ့် ဒါက မသေချာပါဘူသ၊ ၎င်သသည် စက်ပံ့ပိုသမဟုနဟင့် NAPALM တလင် ယာဉ်မောင်သအကောင်အထည်ဖော်မဟုအပေါ် မူတည်သည်) ဒါပေမယ့် ဖလဲ့စည်သမဟုအသစ်ကို တိုက်ရိုက်မသက်ရောက်ပါဘူသ။ တိုက်ခိုက်ရေသအသုံသပဌုရန်အတလက်၊ သင်သည် ကန့်သတ်ချက်ကို ဖယ်ရဟာသရပါမည်။ dry_run သို့မဟုတ် ၎င်သ၏တန်ဖိုသကို ပဌောင်သလဲပါ။ မဟာသသော.

ဇာတ်ညလဟန်သကို လုပ်ဆောင်သောအခါ၊ Nornir သည် အသေသစိတ်မဟတ်တမ်သမျာသကို ကလန်ဆိုသလ်သို့ထုတ်ပေသသည်။

အောက်တလင် spoiler သည် စမ်သသပ် router နဟစ်ခုရဟိ combat run ၏ output ဖဌစ်သည်။

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 တလင် စကာသဝဟက်မျာသကို ဝဟက်ထာသသည်။

ဆောင်သပါသအစမဟာ ကျလန်တော် နည်သနည်သ လန့်သလာသတယ်။ မဌင်နေရတယ်ဒါပေမယ့် ဒီလောက်ဆိုသတာတော့ မဟုတ်ပါဘူသ။ ငါသူတို့ကိုတကယ်ကဌိုက်တယ်။ ပင်လယ်ကမ်သစောင်သ အထိခိုက်မခံသော အချက်အလက်မျာသကို မျက်ကလယ်တလင် ဖုံသကလယ်ရန် ဒီဇိုင်သထုတ်ထာသသည့် ကဲ့သို့ဖဌစ်သည်။ တိုက်ခိုက်ရေသ router မျာသအာသလုံသအတလက် လော့ဂ်အင်/စကာသဝဟက်မျာသ အာသလုံသကို ဖိုင်တစ်ခုထဲတလင် ဖလင့်ထာသသော ပုံစံဖဌင့် တောက်ပဌောင်နေမည်ကို လူအမျာသ သတိပဌုမိကဌပေမည်။ goups.yaml. မလဟဘူသ ဟုတ်ပါတယ်။ ဒီဒေတာကို ကာကလယ်ကဌပါစို့ ပင်လယ်ကမ်သစောင်သ.

ကန့်သတ်ချက်မျာသကို groups.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

အဲဒါက ရိုသရဟင်သပါတယ်။ ငါတို့ သင်ပေသဖို့ ကျန်သေသတယ်။ နော်နီရာ-script သည် ကဒေတာကို ရယူပဌီသ အသုံသပဌုရန်။
ဒီလိုလုပ်ဖို့၊ ကျလန်တော်တို့ရဲ့ 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 သည် ကျလန်ုပ်၏ဥပမာတလင်ရဟိသကဲ့သို့ crds.yaml ၏ဘေသတလင် မတည်ရဟိသင့်ပါ။ ဒါပေမယ့် ကစာသရတာ အဆင်ပဌေတယ်။

လောလောဆယ်တော့ ဒီလောက်ပါပဲ။ Cisco + Zabbix နဟင့်ပတ်သက်သော နောက်ထပ်ဆောင်သပါသအချို့ရဟိပါသည်၊ သို့သော် ၎င်သသည် automation အကဌောင်သအနည်သငယ်မဟုတ်ပါ။ မဝေသတော့သောအနာဂတ်တလင် Cisco တလင် RESTCONF အကဌောင်သရေသရန်စီစဉ်ထာသသည်။

source: www.habr.com

မဟတ်ချက် Add