Load XML with replace pattern failing using PyEZ, works direct

23 views
Skip to first unread message

carlo...@fluidone.com

unread,
Dec 19, 2024, 10:11:06 AM12/19/24
to Junos Python EZ
Hi all,

I'm having trouble replacing a pattern using the documented global NETCONF approach with PyEZ. It's documented here: https://www.juniper.net/documentation/us/en/software/junos/junos-xml-protocol/topics/task/junos-xml-protocol-configuration-data-replacing-patterns.html that the approach for a global pattern replace would be as so:

<rpc>
   <load-configuration>
      <configuration replace-pattern="testing" with="production">
      </configuration>
   </load-configuration>
</rpc>

When I connect to a device using ssh hostname -s netconf, applying the configuration as above in that NETCONF session works without issue.

But when I attempt this using PyEZ as so:

xml = """<rpc>
<load-configuration>
<configuration replace-pattern="testing" with="production">
</configuration>
</load-configuration>
</rpc>"""
cfg.load(xml, format="xml")

I see the following error back in PyEZ:

lxml.etree.XMLSyntaxError: Opening and ending tag mismatch: rpc-reply line 1 and load-configuration-results, line 24, column 30

If I set traceoptions on netconf I see the following:

Dec 19 13:43:52 [NETCONF] - [26796] Incoming: <?xml version="1.0" encoding="UTF-8"?><nc:rpc xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="urn:uuid:a6e7d990-8192-44cb-8298-e9afeba567da"><load-configuration format="xml" action="replace"><configuration><rpc>
                                                                                        <load-configuration>
                                                                                                        <configuration replace-pattern="testing" with="production">
                                                                                                        </configuration>
                                                                                        </load-configuration>
                                                                                </rpc></configuration></load-configuration></nc:rpc>]]>]]>
Dec 19 13:43:52 [NETCONF] - [26796] Outgoing: <nc:rpc-reply  xmlns:junos="http://xml.juniper.net/junos/20.2R0/junos" xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="urn:uuid:a6e7d990-8192-44cb-8298-e9afeba567da">
Dec 19 13:43:52 [NETCONF] - [26796] Outgoing: <load-configuration-results format="xml" action="update">
Dec 19 13:43:52 [NETCONF] - [26796] Outgoing: <nc:rpc-error>
Dec 19 13:43:52 [NETCONF] - [26796] Outgoing: <nc:error-type>protocol</nc:error-type>
Dec 19 13:43:52 [NETCONF] - [26796] Outgoing: <nc:error-tag>operation-failed</nc:error-tag>
Dec 19 13:43:52 [NETCONF] - [26796] Outgoing: <nc:error-severity>error</nc:error-severity>
Dec 19 13:43:52 [NETCONF] - [26796] Outgoing: <nc:error-message>syntax error, expecting &lt;/configuration&gt;</nc:error-message>
Dec 19 13:43:52 [NETCONF] - [26796] Outgoing: <nc:error-info>
Dec 19 13:43:52 [NETCONF] - [26796] Outgoing: <nc:bad-element>rpc</nc:bad-element>


...so it looks as though PyEZ is wrapping the XML in <load-configuration> and <configuration> tags already, and I assume this is what's causing the problem, but I can't see how I would get PyEZ to not send those tags/edit its configuration tag accordingly.

It looks as though the same issue came up in Ansible and they've fixed it by appending the XML as here https://github.com/ansible/ansible/commit/6ada5cc07416713ee20073b66f242ec935549ada but given we would to actually adjust the <configuration> tag to make a global pattern replacement, I can't see that it would fix the problem here either.

Obviously, I could just revert to using ncclient directly instead, but I'd rather not, similarly with using a screen-scraping library or by pull the current configuration's set commands then regex replacing the pattern in python to then create the set commands.

If anyone has any suggestions, that would be great!

Thanks,
Carl
Reply all
Reply to author
Forward
0 new messages