Help us improve your experience.

Let us know what you think.

Do you have time for a two-minute survey?

close
keyboard_arrow_left
list Table of Contents
file_download PDF
{ "lLangCode": "en", "lName": "English", "lCountryCode": "us", "transcode": "en_US" }
English
keyboard_arrow_right

Replace Patterns in Configuration Data Using the NETCONF or Junos XML Protocol

date_range 29-Nov-23

Starting in Junos OS Release 15.1R1, in a NETCONF or Junos XML protocol session with a device running Junos OS, you can replace variables and identifiers in the configuration by including the replace-pattern attribute when performing a <load-configuration> operation. The replace-pattern attribute replaces the given pattern with another pattern either globally or at the indicated hierarchy or object level in the configuration. For example, you can use this feature to find and replace all occurrences of an interface name when a PIC is moved to another slot in the router. The functionality of the attribute is identical to that of the replace pattern configuration mode command in the Junos OS CLI.

Note:

The replace pattern operation can only be used with configuration data formatted as Junos XML tag elements.

To replace a pattern, a client application emits the <rpc> and <load-configuration> tag elements and includes the basic Junos XML tag elements described in Creating, Modifying, or Deleting 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.

Replacing Patterns Globally Within the Configuration

To globally replace a pattern throughout the candidate configuration or open configuration database, include the replace-pattern and with attributes in the opening <configuration> tag.

content_copy zoom_out_map
<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:

content_copy zoom_out_map
<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.

content_copy zoom_out_map
<rpc>
   <get-configuration compare="rollback" rollback="0" format="text">
   </get-configuration>
</rpc>

<rpc-reply xmlns:junos="http://xml.juniper.net/junos/15.1R1/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>

Replacing Patterns Within a Hierarchy Level or Container Object That Has No Identifier

To replace a pattern under a specific hierarchy level including all of its children (or a container object that has children but no identifier), a client application includes the replace-pattern and with attributes in the empty tag that represents the hierarchy level or container object.

content_copy zoom_out_map
<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:

content_copy zoom_out_map
<rpc>
   <load-configuration>
      <configuration>
         <interfaces replace-pattern="fe-0/0/1" with="ge-1/0/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. For example:

content_copy zoom_out_map
<rpc>
   <get-configuration compare="rollback" rollback="0" format="text">
   </get-configuration>
</rpc>

<rpc-reply xmlns:junos="http://xml.juniper.net/junos/15.1R1/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>

Replacing 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, which then encloses the identifier tag element for that object. In the following example, the identifier tag element is <name>:

content_copy zoom_out_map
<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:

content_copy zoom_out_map
<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>

After executing the RPC, you can compare the updated candidate configuration to the active configuration to verify the pattern replacement. For example:

content_copy zoom_out_map
<rpc>
   <get-configuration compare="rollback" rollback="0" format="text">
   </get-configuration>
</rpc>

<rpc-reply xmlns:junos="http://xml.juniper.net/junos/15.1R1/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>
external-footer-nav