Help us improve your experience.

Let us know what you think.

Do you have time for a two-minute survey?

 
 

Use Ansible to Halt, Reboot, or Shut Down Junos Devices

SUMMARY Use the Juniper Networks Ansible modules to halt, reboot, or shut down Junos devices.

Use Ansible to Halt, Reboot, or Shut Down Devices

Juniper Networks provides an Ansible module that you can use to to halt, reboot, or shut down a Junos device. Table 1 outlines the available module.

Table 1: Module to Halt, Reboot, or Shut Down Devices

Content Set

Module Name

juniper.device collection

system

You can use the juniper.device.system module to request the following operations on Junos devices. By default, the module immediately executes the requested operation and performs the operation on all Routing Engines in a dual Routing Engine or Virtual Chassis setup.

  • An immediate system halt, reboot, or shutdown

  • A halt, reboot, or shutdown operation with an optional delay

  • A halt, reboot, or shutdown operation scheduled at a specific date and time

The system module requires one argument, action, which defines the action that the module performs. Table 2 defines the action parameter value that is required to halt, reboot, or shut down a device and provides a brief description of each action as well as the corresponding CLI command. For information about the "zeroize" action, see Use Ansible to Restore a Junos Device to the Factory-Default Configuration Settings.

Table 2: action Parameter Values

Value of action Parameter

Description

Equivalent CLI Command

"halt"

Gracefully shut down the Junos OS software but maintain system power

request system halt

"reboot"

Reboot the Junos OS software

request system reboot

"shutdown"

Gracefully shut down the Junos OS software and power off the Routing Engines

request system power-off

The following Ansible playbook uses the system module with action: "reboot" to immediately reboot all Routing Engines on the hosts in the specified inventory group.

How to Perform a Halt, Reboot, or Shut Down with a Delay or at a Specified Time

You can delay the halt, reboot, or shut down operation by a specified number of minutes. To add a delay, set the optional in_min parameter to the number of minutes that the system should wait before executing the operation. The following task requests a reboot of all Routing Engines in 30 minutes:

You can also schedule the halt, reboot, or shutdown operation at a specific time. To schedule a time, include the at parameter, which takes a string that can be specified in one of the following ways:

  • now—Immediately initiate the halt, reboot, or shut down of the software.

  • +minutes—Number of minutes from now when the requested action is initiated.

  • yymmddhhmm—Absolute time at which to initiate the requested action, specified as year, month, day, hour, and minute.

  • hh:mm—Absolute time on the current day at which to initiate the requested action, specified in 24-hour time.

The following task schedules a system shutdown of all Routing Engines at 22:30 on the current day:

How to Specify the Target Routing Engine

By default, the system module performs the requested operation on all Routing Engines in a dual Routing Engine or Virtual Chassis setup. You can also instruct the module to perform the operation on only the Routing Engine to which the application is connected or to perform the operation on all Routing Engines except the one to which the application is connected.

To specify the Routing Engines, you use the all_re and other_re parameters. Table 3 summarizes the all_re and other_re values that are required to execute the requested operation on specific Routing Engines.

Table 3: Parameters for Specifying Routing Engines

Affected Routing Engines

all_re Parameter

other_re Parameter

All Routing Engines (default)

Omit or set to true

Only the connected Routing Engine

Set to false

All Routing Engines except the Routing Engine to which the application is connected

Set to true

To explicitly indicate that the operation should be performed on all Routing Engines in a dual Routing Engine or Virtual Chassis setup, include the all_re: true argument, which is the default.

To perform the requested action on only the Routing Engine to which the application is connected, include the all_re: false argument.

To perform the requested action on all Routing Engines in the system except for the Routing Engine to which the application is connected, include the other_re: true argument.

How to Reboot or Shut Down a VM Host

On devices that have Routing Engines with VM host support, Junos OS runs as a virtual machine (VM) over a Linux-based host (VM host). The system module supports the vmhost argument, which enables you to reboot or shut down a VM Host.

When you include the action: "reboot" and vmhost: true arguments, the system reboots the host OS and compatible Junos OS on all Routing Engines by executing the <request-vmhost-reboot> RPC, which corresponds to the request vmhost reboot operational mode command.

Similarly, when you include the action: "shutdown" and vmhost: true arguments, the system shuts down the host OS and compatible Junos OS on all Routing Engines by executing the <request-vmhost-poweroff> RPC, which corresponds to the request vmhost power-off operational mode command.

The following playbook performs a VM host reboot, which reboots both the host OS and the guest Junos OS.

Example: Use Ansible to Reboot Junos Devices

The juniper.device.system module enables you to halt, reboot, or shut down a Junos device. This example uses the system module to reboot a Junos device.

Requirements

This example uses the following hardware and software components:

  • Configuration management server running Ansible 2.10 or later with the juniper.device collection installed

  • Junos device with NETCONF enabled and a user account configured with appropriate permissions

  • SSH public/private key pair configured for the appropriate user on the Ansible control node and Junos device

  • Existing Ansible inventory file with required hosts defined

Overview

This example presents an Ansible playbook that uses the juniper.device.system module to reboot a Junos device. The value of the module’s action argument defines the operation to execute on the host.

When calling the module from a playbook, we recommend that you use an interactive prompt to confirm that the user does intend to reboot the specified devices. If a user unintentionally runs the playbook and there is no check, it could adversely affect any networks that require the impacted devices. As a precaution, this playbook uses an interactive prompt to verify that the user intends to reboot the devices and requires that the user manually type 'yes' on the command line to execute the module. If the Confirmation check task fails, the Ansible control node skips the other tasks in the play for that device.

This playbook includes the Check NETCONF connectivity task, which utilizes the ansible.builtin.wait_for module to try to establish a NETCONF session with the Junos device using the default NETCONF port 830. If the control node fails to establish a NETCONF session with the device during playbook execution, then it skips the remaining tasks in the play for that device.

The task that reboots the device executes the system module provided that the confirmation and NETCONF checks were successful. The action argument is set to the value "reboot", which indicates that the software should be rebooted. The in_min: 2 argument instructs the module to wait for the specified number of minutes before executing the reboot command. This provides time for any users to log out of the system.

The task stores the module result in the result variable and notifies two handlers. The pause_for_reboot handler waits a specified amount of time after the reboot operation is initiated to prevent the wait_reboot handler from falsely detecting that the device is online before the reboot takes place. The wait_reboot handler then tries to establish a session with the device to verify that the device comes back online after the reboot. The wait_time_after_reboot variable defines the length of time that the control node attempts to reconnect with the device.

Configuration

Create and Execute the Ansible Playbook

Step-by-Step Procedure

To create a playbook that uses the system module to reboot a Junos device:

  1. Include the boilerplate for the playbook and this play, which executes the modules locally.

  2. Define or import any necessary variables.

  3. Create an interactive prompt to prevent users from accidentally executing the module without first understanding the implications.

  4. Create the task that confirms the user’s intent.

  5. (Optional) Create a task to verify NETCONF connectivity.

  6. Create the task to reboot the device after a specified number of minutes and then notify the handlers.

  7. (Optional) Create a task to print the response.

  8. Create the handler that pauses after rebooting and the handler that verifies that the device comes back online after rebooting.

    The handler names should be the same as those referenced in the reboot task.

Results

On the Ansible control node, review the completed playbook. If the playbook does not display the intended code, repeat the instructions in this example to correct the playbook.

Execute the Playbook

Procedure

Step-by-Step Procedure

To execute the playbook:

  • Issue the ansible-playbook command on the control node, and provide the playbook path and any desired options.

Verification

Verify the Reboot

Purpose

Verify that the Junos device successfully rebooted.

Action

When you execute the playbook, review the output of the wait_reboot task for each device.

Meaning

The wait_reboot result indicates whether the control node successfully established a session with the device after it rebooted. If the result indicates success, the device is online.

Change History Table

Feature support is determined by the platform and release you are using. Use Feature Explorer to determine if a feature is supported on your platform.

Release
Description
1.0.3
Starting with juniper.device collection Release 1.0.3, the system module supports shutting down a VM Host.