PyEZ Config Load with XML

831 views
Skip to first unread message

Sotiris Ioannou

unread,
Aug 20, 2015, 11:26:47 AM8/20/15
to Junos Python EZ
Hi all,

Does anyone have an example of loading configuration using PyEZ and an XML file?
Can someone provide an example?

Also using an xml format is it possible to delete existing configuration on the device?

Thanks,

Sotiris

Nitin Kumar

unread,
Aug 21, 2015, 5:09:14 AM8/21/15
to Sotiris Ioannou, Junos Python EZ



Does anyone have an example of loading configuration using PyEZ and an XML file?
Can someone provide an example?


cu = Config(dev)
cu.load(path=‘path_to_xml_config’)
cu.commit()


Also using an xml format is it possible to delete existing configuration on the device?

You can follow below example to use delete action in xml config file.

xml = """<policy-options>
          <policy-statement action="delete">
            <name>F5-in</name>
            <term>
                <name>testing</name>
                <then>
                    <accept/>
                </then>
            </term>
            <from>
                <protocol>mpls</protocol>
            </from>
        </policy-statement>
        </policy-options>"""

cu = Config(dev)
cu.load(xml, format='xml')
cu.commit()

Sotiris Ioannou

unread,
Aug 21, 2015, 7:23:12 AM8/21/15
to Junos Python EZ, sotiris...@gmail.com, nit...@juniper.net
Hi Nitin,

I have tried the delete option and it doesn't work. The netconf module in the log shows replace (the configuration commits no errors is not removed from the configuration though).
UI_NETCONF_CMD: User 'root' used NETCONF client to run command 'load-configuration action="replace" format="xml"'

Is this only supported on certain type of platforms I'm using a vSRX for the testing.
Also is there a document that describes at what level of the xml you can apply an action?

Thanks,

Sotiris

Sotiris Ioannou

unread,
Aug 21, 2015, 8:25:02 AM8/21/15
to Junos Python EZ, sotiris...@gmail.com, nit...@juniper.net
Hi Nitin,

And anyone reaching this post, in my search for an answer I came across the following information:

Deleting Configuration Elements Using NETCONF (Juniper Doc):

http://www.juniper.net/documentation/en_US/junos14.2/topics/task/configuration/netconf-configuration-elements-deleting.html

Changing Individual Configuration Elements Using NETCONF (Juniper Doc):

http://www.juniper.net/documentation/en_US/junos14.2/topics/task/configuration/netconf-configuration-elements-changing.html

NETCONF XML Management Protocol Developer Guide (Juniper Doc):

http://www.juniper.net/techpubs/en_US/junos14.2/information-products/pathway-pages/netconf-guide/netconf.html#configuration

So to delete the configuration under the inet stanza of an interface do the following:
<python code>
from jnpr.junos import Device
from pprint import pprint
from jnpr.junos.utils.config import Config
from jnpr.junos.exception import *
import paramiko
cpe = Device (host="IP-ADDRESS", user="root", password="pass")
cpe.bind( cu=Config )
xml= """
            <configuration>
            <interfaces>
            <interface>
            <name>ge-0/0/1</name>
            <unit>
            <name>0</name>
            <family>
            <inet operation="delete">
            </inet>
            </family>
            </unit>
            </interface>
            </interfaces>
            </configuration>"""
cu.load(xml,format='xml')
cu.commit
<python code>

Pay attention to the operation keyword that is the one you need to delete the configuration.

To find the xml structure run the command show configuration | display xml  on you router to get the details and just reference the part you are interested in.


Regards,

Sotiris





Reply all
Reply to author
Forward
0 new messages