Replace Patterns in Configuration Data Using the NETCONF or Junos XML Protocol
NETCONF and Junos XML protocol client applications can replace variables and
identifiers in the configuration when performing a <load-configuration>
operation.
A NETCONF or Junos XML protocol client application can replace variables and identifiers in the configuration of devices running Junos OS or devices running Junos OS Evolved. For example, you might need to replace all occurrences of an interface name when a PIC is moved to another slot in the router.
To replace a pattern, a client application uses the
<load-configuration> operation with the
replace-pattern attribute. The replace-pattern
attribute replaces the existing pattern with the new pattern. The scope of the replacement
can be global or at a specified hierarchy or object level in the configuration. The
functionality of the replace-pattern attribute is identical to that of the
replace pattern configuration mode command in the Junos OS CLI.
To use the replace pattern operations, you must use Junos XML elements for the configuration data format.
To replace a pattern, a client application emits the <rpc> and
<load-configuration> elements and includes the basic Junos XML
tag elements described in Create, Modify, or Delete Configuration Elements Using the Junos XML Protocol. At the hierarchy or object level where you want to replace the pattern, include the
following attributes:
-
replace-pattern—Pattern to replace. -
with—Replacement pattern. -
upto—(Optional) Number of occurrences to replace. If you omit this attribute or set it to zero, the device replaces all instances of the pattern within the specified scope.
The placement of the attributes within the configuration determines the scope of the replacement as described in the following sections.
Replace Patterns Globally Within the Configuration
To replace a pattern globally throughout the candidate configuration or open
configuration database, include the replace-pattern and
with attributes in the opening
<configuration> tag. You can optionally include the
up-to attribute to replace only a specified number of
occurrences.
<rpc>
<load-configuration>
<configuration replace-pattern="pattern1" with="pattern2" [upto="n"]>
</configuration>
</load-configuration>
</rpc>For example, the following RPC replaces all instances of 172.17.1.5 with 172.16.1.1:
<rpc>
<load-configuration>
<configuration replace-pattern="172.17.1.5" with="172.16.1.1"/>
</configuration>
</load-configuration>
</rpc>After executing the RPC, you can compare the updated candidate configuration to the active configuration to verify the pattern replacement. You must commit the configuration for the changes to take effect.
<rpc> <get-configuration compare="rollback" rollback="0" format="text"> </get-configuration> </rpc> <rpc-reply xmlns:junos="http://xml.juniper.net/junos/24.4R1/junos"> <configuration-information> <configuration-output> [edit groups global system ntp] - boot-server 172.17.1.5; + boot-server 172.16.1.1; [edit groups global system ntp] + server 172.16.1.1; - server 172.17.1.5; </configuration-output> </configuration-information> </rpc-reply>
Replace Patterns Within a Hierarchy Level or Container Object That Has No Identifier
A client application can replace a pattern under a specific hierarchy level including
all of its children (or a container object that has children but no identifier). To
replace the pattern within a specific hierarchy level, a client application includes
the replace-pattern and with attributes in the
empty tag that represents the hierarchy level or container object.
<rpc>
<load-configuration>
<configuration>
<!-- opening tag for each parent element -->
<level-or-object replace-pattern="pattern1" with="pattern2" [upto="n"]/>
<!-- closing tag for each parent element -->
</configuration>
</load-configuration>
</rpc>The following RPC replaces instances of fe-0/0/1 with ge-1/0/1 at the [edit
interfaces] hierarchy level:
<rpc>
<load-configuration>
<configuration>
<interfaces replace-pattern="fe-0/0/1" with="ge-1/0/1"/>
</configuration>
</load-configuration>
</rpc>Compare the updated candidate configuration to the active configuration to verify the pattern replacement. For example:
<rpc>
<get-configuration compare="rollback" rollback="0" format="text">
</get-configuration>
</rpc>
<rpc-reply xmlns:junos="http://xml.juniper.net/junos/24.4R1/junos">
<configuration-information>
<configuration-output>
[edit interfaces]
- fe-0/0/1 {
- unit 0 {
- family inet {
- address 10.0.1.1/27;
- }
- }
- }
+ ge-1/0/1 {
+ unit 0 {
+ family inet {
+ address 10.0.1.1/27;
+ }
+ }
+ }
</configuration-output>
</configuration-information>
</rpc-reply>Replace Patterns for a Configuration Object That Has an Identifier
To replace a pattern for a configuration object that has an identifier, a client
application includes the replace-pattern and with
attributes in the opening tag for the object. Within the container tag, the
application also includes the identifier element for that object. In the following
example, the identifier tag is <name>:
<rpc>
<load-configuration>
<configuration>
<!-- opening tag for each parent element -->
<container-tag replace-pattern="pattern1" with="pattern2" [upto="n"]>
<name>identifier</name>
</container-tag>
<!-- closing tag for each parent element -->
</configuration>
</load-configuration>
</rpc>The following RPC replaces instances of 4.5 with 4.1, but only for the fe-0/0/2
interface under the [edit interfaces] hierarchy:
<rpc>
<load-configuration>
<configuration>
<interfaces>
<interface replace-pattern="4.5" with="4.1">
<name>fe-0/0/2</name>
</interface>
</interfaces>
</configuration>
</load-configuration>
</rpc>You can compare the updated candidate configuration to the active configuration to verify the pattern replacement. For example:
<rpc> <get-configuration compare="rollback" rollback="0" format="text"> </get-configuration> </rpc> <rpc-reply xmlns:junos="http://xml.juniper.net/junos/24.4R1/junos"> <configuration-information> <configuration-output> [edit interfaces fe-0/0/2 unit 0 family inet] + address 10.0.4.1/30; - address 10.0.4.5/30; </configuration-output> </configuration-information>