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:
salt 'target' junos.cli 'command' dest=dest format=format
The salt.states.junos.cli
state function supports
the following syntaxes:
command: junos.cli: - dest: dest - format: format
id: junos.cli: - name: command - dest: dest - format: format
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. Specifytext
orxml
.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:
saltuser@salt-master:~$ sudo salt 'router1' junos.cli 'show system uptime' router1: ---------- message: Current time: 2019-07-23 11:04:18 PDT Time Source: NTP CLOCK System booted: 2019-07-01 07:03:56 PDT (3w1d 04:00 ago) Protocols started: 2019-07-01 07:07:27 PDT (3w1d 03:56 ago) Last configured: 2019-07-18 16:16:33 PDT (4d 18:47 ago) by saltuser 11:04AM up 22 days, 4 hrs, 0 users, load averages: 0.02, 0.05, 0.06 out: True
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.
saltuser@salt-master:~$ cat /srv/salt/junos_cli.sls show system uptime: junos.cli: - dest: /var/log/salt/output/{{ grains['id'] }}_junos_system_uptime.txt show version: junos.cli: - dest: /var/log/salt/output/{{ grains['id'] }}_junos_show_version.txt
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.
saltuser@salt-master:~$ sudo salt 'router1' state.apply junos_cli router1: ---------- ID: show system uptime Function: junos.cli Result: True Comment: Started: 21:18:12.130459 Duration: 911.207 ms Changes: ---------- message: Current time: 2019-07-26 14:18:12 PDT Time Source: NTP CLOCK System booted: 2019-07-01 07:03:56 PDT (3w4d 07:14 ago) Protocols started: 2019-07-01 07:07:27 PDT (3w4d 07:10 ago) Last configured: 2019-07-24 10:17:34 PDT (2d 04:00 ago) by saltuser 2:18PM up 25 days, 7:14, 1 user, load averages: 0.09, 0.06, 0.07 out: True ---------- ID: show version Function: junos.cli Result: True Comment: Started: 21:18:13.041796 Duration: 968.359 ms Changes: ---------- message: Hostname: router1 Model: mx104 Junos: 18.4R1.8 JUNOS Base OS boot [18.4R1.8] JUNOS Base OS Software Suite [18.4R1.8] JUNOS Crypto Software Suite [18.4R1.8] JUNOS Packet Forwarding Engine Support (TRIO) [18.4R1.8] JUNOS Web Management [18.4R1.8] JUNOS Online Documentation [18.4R1.8] JUNOS SDN Software Suite [18.4R1.8] JUNOS Services Application Level Gateways [18.4R1.8] JUNOS Services COS [18.4R1.8] JUNOS Services Jflow Container package [18.4R1.8] JUNOS Services Stateful Firewall [18.4R1.8] JUNOS Services NAT [18.4R1.8] JUNOS Services RPM [18.4R1.8] JUNOS Services SOFTWIRE [18.4R1.8] JUNOS Services Captive Portal and Content Delivery Container package [18.4R1.8] JUNOS Macsec Software Suite [18.4R1.8] JUNOS Services Crypto [18.4R1.8] JUNOS Services IPSec [18.4R1.8] JUNOS DP Crypto Software Software Suite [18.4R1.8] JUNOS py-base-powerpc [18.4R1.8] JUNOS py-extensions-powerpc [18.4R1.8] JUNOS jsd [powerpc-18.4R1.8-jet-1] JUNOS Kernel Software Suite [18.4R1.8] JUNOS Routing Software Suite [18.4R1.8] out: True Summary for router1 ------------ Succeeded: 2 (changed=2) Failed: 0 ------------ Total states run: 2 Total run time: 1.880 s
saltuser@minion:~$ ls /var/log/salt/output router1_junos_show_version.txt router1_junos_system_uptime.txt
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.
saltuser@salt-master:~$ sudo salt '*' state.apply junos_cli router1: ---------- ID: show system uptime Function: junos.cli ... router2: ---------- ID: show system uptime Function: junos.cli ...
saltuser@minion:~$ ls /var/log/salt/output router1_junos_show_version.txt router1_junos_system_uptime.txt router2_junos_show_version.txt router2_junos_system_uptime.txt
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:
Get OSPF neighbor information: junos.cli: - name: show ospf neighbor - format: text Install OSPF config : junos.install_config: - name: salt://configs/junos_ospf_config.conf Get OSPF neighbor information after configuration changes: junos.cli: - name: show ospf neighbor - format: text
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:
saltuser@salt-master:~$ sudo salt 'router1' junos.cli 'show system uptime' dest='/home/saltuser/cli_output.txt'
saltuser@minion:~$ ls cli_output.txt
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.
saltuser@salt-master:~$ sudo salt 'router1' junos.cli 'show system uptime' format=xml dest=/home/saltuser/show-system-uptime.xml router1: ---------- message: ---------- system-uptime-information: ---------- current-time: ---------- date-time: 2019-07-29 15:38:21 PDT last-configured-time: ---------- date-time: 2019-07-24 10:17:34 PDT time-length: 5d 05:20 user: saltuser protocols-started-time: ---------- date-time: 2019-07-01 07:07:27 PDT time-length: 4w0d 08:30 system-booted-time: ---------- date-time: 2019-07-01 07:03:56 PDT time-length: 4w0d 08:34 time-source: NTP CLOCK uptime-information: ---------- active-user-count: 1 date-time: 3:38PM load-average-1: 0.04 load-average-15: 0.04 load-average-5: 0.05 up-time: 28 days, 8:34 out: True
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.
saltuser@minion:~$ cat /home/saltuser/show-system-uptime.xml <system-uptime-information><current-time><date-time seconds="1564439901">2019-07-29 15:38:21 PDT</date-time></current-time><time-source> NTP CLOCK </time-source><system-booted-time><date-time seconds="1561989836">2019-07-01 07:03:56 PDT</date-time><time-length seconds="2450065">4w0d 08:34</time-length></system-booted-time><protocols-started-time><date-time seconds="1561990047">2019-07-01 07:07:27 PDT</date-time><time-length seconds="2449854">4w0d 08:30</time-length></protocols-started-time><last-configured-time><date-time seconds="1563988654">2019-07-24 10:17:34 PDT</date-time><time-length seconds="451247">5d 05:20</time-length><user>saltuser</user></last-configured-time><uptime-information><date-time seconds="1564439902"> 3:38PM </date-time><up-time seconds="2450096"> 28 days, 8:34 </up-time><active-user-count format="1 user"> 1 </active-user-count><load-average-1> 0.04 </load-average-1><load-average-5> 0.05 </load-average-5><load-average-15> 0.04 </load-average-15></uptime-information></system-uptime-information>