Automation

Expand all | Collapse all

Using PyEz to configure interface results in error: bad element: interface

Jump to Best Answer
  • 1.  Using PyEz to configure interface results in error: bad element: interface

    Posted 22 days ago

    Hi,

    I've just begun my road to victory with python and pyez. So there might be some basic failure in my understanding on what and how this should be working.

    First out, I deployed set vlans config, no issue at all, works just fine.

    Then I've added "set interface ge-0/0/5.<XXX> vlan-id <XXX>. This just wont load. If I run it manually on the cli it works fine. 

    Here is my code snippet!

     

    a_device = Device(host=my_host, gather_facts=Falseuser=usrname, password=usrpasswd)
      a_device.open()
      Device(a_device)
      cfg = Config(a_device)
      cfg.lock()
      if set_cmds:
        print(set_cmds)
        for cmd in set_cmds:
          
          print("Debug... " + cmd)
          #cfg.load(cmd, format="set", merge=True) <-- This doesnt work.
          #Adding the cfg static, to rule out everything else.
          cfg.load("set interface ge-0/0/5 unit 122 vlan-id 122", format="set", merge=True) <-- it breaks here!
          print("Did it pass?")
          #if it passed, can i do cfg.load with more set-lines?
          #cfg.load("set interface ge-0/0/5 unit 11 vlan-id 11", format="set", merge=True)
     
    I can see that the commands in my "set_cmds" list is just fine if i print them. Also the set vlan works with the "set_cmds" list. So thats not the problem.
    It says "bad element" when trying to set the interface:
    Traceback (most recent call last):
    File "set-vlan-MAIN.py", line 150, in <module>
    jnpr_vlanarg_ssh_keys(host, buildvlancfg())
    File "set-vlan-MAIN.py", line 91, in jnpr_vlanarg_ssh_keys
    cfg.load("set interface ge-0/0/5 unit 122 vlan-id 122", format="set", merge=True)
    File "/usr/lib/python3.8/site-packages/jnpr/junos/utils/config.py", line 579, in load
    return try_load(rpc_contents, rpc_xattrs, ignore_warning=ignore_warning)
    File "/usr/lib/python3.8/site-packages/jnpr/junos/utils/config.py", line 496, in try_load
    raise ConfigLoadError(cmd=err.cmd, rsp=err.rsp, errs=err.errs)
    jnpr.junos.exception.ConfigLoadError: ConfigLoadError(severity: error, bad_element: interface, message: error: syntax error)
     
    Any ideas why?
    I've tried to find out in the docs but cant see a clear reason on this matter.
    Thanks in advance for any input!
    //Rob


  • 2.  Re: Using PyEz to configure interface results in error: bad element: interface
    Best Answer

    Posted 22 days ago

    Hello,

     

    Just use the correct CLI command (as you do not have autocompletion when using the scripts) and it will work.

     

    Your case:   "set interface ge-0/0/5.<XXX> vlan-id <XXX>."

    Correct:  "set interfaces ge-0/0/5.<XXX> vlan-id <XXX>.

                                         ^^

     

    BR,

    Andrei

     

    Accept as Solution = cool !
    Accept as Solution+Kudo = You are a Star !

     



  • 3.  Re: Using PyEz to configure interface results in error: bad element: interface

    Posted 21 days ago

    oh god, your right, i didnt see that one, a #### typo!!

     

    Changes on device: 172.20.5.153

    [edit interfaces ge-0/0/5]
    + unit 122 {
    + vlan-id 122;
    + }

    Diff exists, push to device? y/n y
    Pushing configuration to device 172.20.5.153

     

    Thanks a lot, i was looking at all other kind of things as the source of this issue 🙂

    Now i might be able to get the first version running of my script to simplify deployment of new networks.

    optional arguments:
    -h, --help show this help message and exit
    --vid VID Enter: a numeric vlanid
    --vname VNAME Enter: DMZ<id> or LAN<id>
    --vdesc VDESC Enter: SystemX_LAN
    --devfile DEVFILE Enter: mydevicelistfile.txt (device1.dns.net,ip1,ip2), overrides devicelist
    --devlist DEVLIST Enter: device1.dns.net,ip1,ip2
    --fwprod FWPROD Enter interface address: 10.0.0.1/30
    --fwdev FWDEV Enter interface address: 10.0.0.1/30

     

    //Rob