Help us improve your experience.

Let us know what you think.

Do you have time for a two-minute survey?

 
 

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

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.

For example, the following RPC replaces all instances of 172.17.1.5 with 172.16.1.1:

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.

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.

The following RPC replaces instances of fe-0/0/1 with ge-1/0/1 at the [edit interfaces] hierarchy level:

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

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

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:

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