Help us improve your experience.

Let us know what you think.

Do you have time for a two-minute survey?

 
 

Using Salt to Retrieve a Junos OS Configuration

Juniper Networks provides support for using Salt to manage devices running Junos OS, and the Junos execution and state modules (for Salt) define functions that enable you to retrieve the configuration from managed devices. You can use the salt.modules.junos.rpc execution function or the salt.states.junos.rpc state function to execute the get_config RPC to retrieve configuration data from a device running Junos OS.

You can use the get_config RPC to retrieve the complete configuration or a subset of the configuration from the candidate, committed, or ephemeral configuration databases. You can return data for the pre-inheritance or post-inheritance configuration. In addition, you can return the data in several different formats and save the data to a file on the proxy minion server.

How to Retrieve the Complete Candidate Configuration

To retrieve the complete candidate configuration from a device running Junos OS, call the junos.rpc function, and execute the get_config RPC.

Similarly, you can define a Salt state file that retrieves the configuration. The following sample state file retrieves the complete candidate configuration for the target devices in text format:

When you apply the state, it displays the candidate configuration for each target device.

How to Specify the Source Database for the Configuration Data

By default, the get_config RPC retrieves configuration data from the candidate configuration database. You can also retrieve data from the committed configuration database or the ephemeral configuration database by including the database parameter with the appropriate value.

Committed Configuration Database

To retrieve data from the committed configuration database, set the database argument equal to committed. For example:

Ephemeral Configuration Database

You can also use Salt to retrieve data from the ephemeral configuration database on devices that support this database. When you retrieve configuration data from the shared configuration database, by default, the results do not include data from the ephemeral configuration database.

To retrieve data from the default instance of the ephemeral configuration database, include the database argument and set it equal to ephemeral.

To retrieve data from a user-defined instance of the ephemeral configuration database, set the database argument equal to ephemeral, and set the ephemeral-instance argument to the name of the ephemeral instance.

How to Specify the Scope of the Configuration Data to Return

In addition to retrieving the complete Junos OS configuration, the get_config RPC can retrieve a subset of the configuration by using the filter parameter. The filter parameter takes a string containing the subtree filter that selects the configuration statements to return. The subtree filter returns the configuration data that matches the selection criteria.

To request multiple hierarchies, the filter string must include the <configuration> root element. Otherwise, the value of filter must represent all levels of the configuration hierarchy starting just under the root <configuration> element down to the hierarchy to display. To select a subtree, include the empty tag for that hierarchy level. To return a specific object, include a content match node that defines the element and value to match.

The following command retrieves and prints the configuration at the [edit interfaces] and [edit protocols] hierarchy levels in the candidate configuration:

The following command retrieves and prints the configuration at the [edit system services] hierarchy level:

The following state file retrieves the <name> element for each <interface> element at the <interfaces> hierarchy level in the post-inheritance candidate configuration:

Applying the state returns the list of interface names.

Similarly, the following example retrieves the subtree for the ge-1/0/1 interface:

How to Specify the Format of the Configuration Data to Return

By default, the junos.rpc function returns the RPC output in XML format for both the data displayed in standard output as well as the data saved to the destination file, if specified. To specify an output format, include the format argument, and set the value equal to the desired format. To request text format or Junos XML elements, use text or xml, respectively. To save the configuration data to a destination file in JSON format, specify json.

The following command saves the configuration data to the specified file and also displays it in standard output in text format:

The following command saves the configuration data to the specified file in JSON format:

How to Specify Inheritance and Groups Options

The get_config RPC retrieves data from the pre-inheritance configuration, in which the <groups>, <apply-groups>, <apply-groups-except>, and <interface-range> tags are separate elements in the configuration output. To retrieve data from the post-inheritance configuration, which displays statements that are inherited from user-defined groups and ranges as children of the inheriting statements, you can include the inherit=inherit argument. If you also include the groups=groups argument, the text and XML-formatted output will indicate the group from which a statement was inherited.

For example, the following command retrieves the configuration at the [edit system services] hierarchy level from the post-inheritance candidate configuration. In this case, if the configuration also contains statements configured at the [edit groups global system services] hierarchy level, those statements are inherited at the [edit system services] hierarchy level in the post-inheritance configuration and returned in the configuration data.

In the destination file, the elements that are inherited from a group include the group attribute. The group attribute value indicates the group from which the element was inherited.

How to Save Retrieved Configuration Data to a File

When you execute the junos.rpc function, you can save the returned data in a file on the proxy minion server by including the dest argument and specifying the path of the destination file. If you do not specify an absolute path, the path is relative to the top-level root (/) directory. If the target output file already exists, the new file overwrites the old file.

To retrieve configuration data from a device running Junos OS and save the output to a file for later reference, execute the get_config RPC and include the dest parameter. The following command retrieves the complete configuration, displays the data in standard output, and also saves the data in the specified file on the proxy minion server:

On the proxy minion server, the configuration data is saved to the specified file.

Similarly, you can define a Salt state that retrieves configuration data and saves it in a file. The following sample state file retrieves the complete configuration and writes the data to both standard output and to the specified file on the proxy minion server:

In this case, the proxy identifier stored in the id grain is included in the destination filename to distinguish the output files when the state is applied to multiple targets. When you apply the state to devices that match the os_family:junos grain, it saves the configuration for each device to its own file on the proxy minion server.

Each device configuration is saved in a separate file on the proxy minion server.