Help us improve your experience.

Let us know what you think.

Do you have time for a two-minute survey?

 
 

Using Salt to Execute Operational Commands on Devices Running Junos OS

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 perform operational and configuration tasks on the managed devices. The salt.modules.junos.cli execution function and the salt.states.junos.cli state function enable you to execute operational mode commands on devices running Junos OS to perform operations or retrieve information.

The following sections discuss how to use the functions, parse the device response, specify the output format, and save the output to a file.

junos.cli Function Syntax

The salt.modules.junos.cli execution function syntax is:

The salt.states.junos.cli state function supports the following syntaxes:

where:

  • command—Operational mode command to execute on the device running Junos OS, for example, show interfaces fxp0.0 terse.

  • dest—(Optional) Path of the destination file on the proxy minion server to which the command output is written. If you do not specify an absolute path on the target device, the path is relative to the top-level root (/) directory.

  • format—(Optional) Format of the command output. Specify text or xml.

  • id—User-defined identifier for the state declaration.

How to Execute Commands with the junos.cli Execution Function

The salt.modules.junos.cli execution function enables you to execute a CLI command on a device running Junos OS. The function requires one argument, which is the command to execute. If the operation is successful, the command returns out: True, and the message key includes the command output.

For example, the following command executes the show system uptime operational mode command on the target device and displays the command output in standard output:

How to Execute Commands with the junos.cli State Function

You can use the salt.states.junos.cli state function within a Salt state file to execute one or more operational commands on a device running Junos OS. You can define the command as the first line of the data structure, or you can define it within the function’s argument list. If you need to execute the same command multiple times within the same state file, you must define the command within the argument list.

The following state file uses the junos.cli state function to execute two commands on the target device and save the output for each command in a separate file. 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, the Salt master displays the command output in standard output and also saves the output to the corresponding destination file on the proxy minion server.

If the state is applied to multiple targets, Salt generates different output files for each target on the proxy minion server in this case, because the defined filename references a unique identifier.

State files cannot use the same state identifier multiple times in a file. Therefore, if you want to execute the same command multiple times in a state file, for example, before and after you modify the configuration, you must define the command in the function’s argument list. For example:

How to Save the Command Output to a File

When you execute the junos.cli 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 an output file already exists with the target name, the new file overwrites the old file.

The dest argument does not save the entire Salt command response in the file. It only saves the command output contained within the message key.

The following command displays the output from the show system uptime command and also saves it to the specified file on the proxy minion server:

How to Specify the Format for the Command Output

By default, the junos.cli function returns the command output in text format for both the data displayed in standard output as well as the data saved to the destination file, if specified. You can also return the data in XML format. To specify XML format, include the format argument, and set the value equal to xml.

The following command executes the show system uptime command and returns the data as XML. In standard output, the elements are displayed in a hierarchy without the traditional brackets enclosing each element name.

The output in the destination file on the proxy minion server displays the same Junos XML tags that would be emitted if you execute the show system uptime | display xml command in the Junos OS CLI.