Use Salt to Reboot or Shut Down 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.shutdown execution function
and the salt.states.junos.shutdown state function enable
you to use Salt to reboot or power down a device running Junos OS.
This topic discusses how to use Salt to reboot or power down a device running Junos OS, execute the operation after a delay, or schedule the operation at a specific time.
junos.shutdown Function Overview
You can use the junos.shutdown function to request the following operations on devices running
Junos OS:
An immediate system reboot or shutdown
A reboot or shutdown operation with an optional delay
A reboot or shutdown operation scheduled at a specific date and time
By default, the junos.shutdown function
executes the requested operation on all Routing Engines, if in a dual
Routing Engine or Virtual Chassis setup. The junos.shutdown function requires one argument, which is the action to perform.
You must explicitly include either the reboot or the shutdown parameter and set it
equal to True.
The salt.modules.junos.shutdown execution function
syntax is:
salt 'target' junos.shutdown (reboot | shutdown)=True (in_min=minutes | at=\'time\')
The salt.states.junos.shutdown state function syntax
is:
id:
junos.shutdown:
- (reboot | shutdown): True
- at: 'time'
- in_min: minutes
where:
at=\'time\'—(Optional) Time at which to schedule the operation.id—User-defined identifier for the state declaration.in_min=minutes—(Optional) Number of minutes to delay the reboot or shutdown operation.(reboot | shutdown)=True—Operation to perform. You must explicitly set either therebootor theshutdownparameter toTrueto specify whether to reboot or shut down the device, respectively.
How to Use the junos.shutdown Execution Function
The salt.modules.junos.shutdown execution function
enables you to reboot or power down one or more devices running Junos
OS from the Salt master command line.
For example, the following command immediately reboots all Routing Engines on the target device:
saltuser@salt-master:~$ sudo salt 'router1' junos.shutdown reboot=True
router1:
----------
message:
Successfully powered off/rebooted.
out:
True
Similarly, the following command powers down the device:
saltuser@salt-master:~$ sudo salt 'router1' junos.shutdown shutdown=True
router1:
----------
message:
Successfully powered off/rebooted.
out:
True
The following command immediately reboots all devices that match
on the os_family:junos grain, which is
useful when you want to apply the operation to all managed devices
running Junos OS:
saltuser@salt-master:~$ sudo salt -G 'os_family:junos' junos.shutdown reboot=True
router1:
----------
message:
Successfully powered off/rebooted.
out:
True
router2:
----------
message:
Successfully powered off/rebooted.
out:
TrueHow to Use the junos.shutdown State Function
You can use the salt.states.junos.shutdown state function within
a Salt state file to reboot or power down one or more devices running
Junos OS.
The following state file immediately reboots all Routing Engines on the target devices to which the state is applied:
saltuser@salt-master:~$ cat /srv/salt/junos_reboot.sls
Reboot all REs now:
junos.shutdown:
- reboot: True
When you apply the state to the target, the device immediately performs the operation.
saltuser@salt-master:~$ sudo salt 'router1' state.apply junos_reboot.sls
router1:
----------
ID: Reboot all REs now
Function: junos.shutdown
Result: True
Comment:
Started: 21:54:51.158521
Duration: 1141.496 ms
Changes:
----------
message:
Successfully powered off/rebooted.
out:
True
Summary for router1
------------
Succeeded: 1 (changed=1)
Failed: 0
------------
Total states run: 1
Total run time: 1.141 s
Similarly, the following state file powers off the target devices:
saltuser@salt-master:~$ cat /srv/salt/junos_shutdown.sls
Shut down device running Junos OS:
junos.shutdown:
- shutdown: TrueHow to Perform a Reboot or Shutdown Operation with a Delay
The default behavior of the junos.shutdown function is to immediately execute the reboot or shutdown operation.
You can also request a delay before the operation executes.
To delay the reboot or shutdown operation by a specified number
of minutes, set the optional in_min parameter
to the number of minutes that the target device running Junos OS should
wait before executing the operation. The following command requests
a reboot of all Routing Engines in 2 minutes:
saltuser@salt-master:~$ sudo salt 'router1' junos.shutdown reboot=True in_min=2
router1:
----------
message:
Successfully powered off/rebooted.
out:
True
The target device broadcasts messages about the impending reboot to any users logged into the system. After the specified amount of time has passed, the system reboots.
user@router1> *** System shutdown message from saltuser@router1 *** System going down in 2 minutes
How to Perform a Reboot or Shutdown Operation at a Specified Time
The default behavior of the junos.shutdown function is to immediately execute the reboot or shutdown operation.
You can also schedule the operation at a particular date and time.
To schedule the operation at a specific time, include the at parameter, which takes a string that can be specified
in one of the following ways:
now—Stop or reboot the software immediately.+minutes—Number of minutes from now to perform the operation.yymmddhhmm—Absolute time at which to perform the operation, specified as year, month, day, hour, and minute.hh:mm—Absolute time on the current day at which to perform the operation, specified in 24-hour time.
The proxy minion server requires Junos PyEZ Release 2.3.0
or later to use the at parameter when you
specify shutdown=True.
For example, the following Salt state file schedules a system reboot of all Routing Engines at 22:30 on the current day:
reboot:
junos.shutdown:
- reboot: True
- at: '22:30'
When you apply the state to the target, the device schedules the reboot for the specified time.
saltuser@salt-master:~$ sudo salt 'router1' state.apply junos_reboot.sls
router1:
----------
ID: reboot
Function: junos.shutdown
Result: True
Comment:
Started: 17:23:27.368341
Duration: 173.988 ms
Changes:
----------
message:
Successfully powered off/rebooted.
out:
True
Summary for router1
------------
Succeeded: 1 (changed=1)
Failed: 0
------------
Total states run: 1
Total run time: 173.988 msYou can view the pending reboot on the device with the show system reboot command.
saltuser@salt-master:~$ sudo salt 'router1' junos.cli 'show system reboot'
router1:
----------
message:
reboot requested by saltuser at Wed Jul 31 22:30:00 2019
[process id 2313]
out:
True
When you execute the same operation on the Salt master command
line, Salt passes the CLI input through PyYAML to ensure it is loaded as a proper Python data type. In this case,
the at value might be parsed incorrectly.
To use the junos.shutdown execution function
with the at argument, you can provide the
value using one of the following methods:
saltuser@salt-master:~$ sudo salt 'router1' junos.shutdown reboot=True at=\'22:30\' saltuser@salt-master:~$ sudo salt 'router1' junos.shutdown reboot=True at="'22:30'" saltuser@salt-master:~$ sudo salt 'router1' junos.shutdown reboot=True "at='22:30'" saltuser@salt-master:~$ sudo salt 'router1' junos.shutdown reboot=True at="22:30" --no-parse=at