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 supports using Ansible to manage Junos devices and provides Ansible modules that enable you to halt, reboot, or shut down a device. Table 1 outlines the available modules.

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

Content Set

Module Name

juniper.device collection

system

Juniper.junos role

juniper_junos_system

Note:

Starting in Juniper.junos Release 2.0.0, the juniper_junos_system module replaces the functionality of the junos_shutdown and junos_zeroize modules.

You can use the modules to request the following operations on Junos devices. By default, the modules immediately execute the requested operation and perform 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 and juniper_junos_system modules require 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: system and juniper_junos_system 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

To delay the halt, reboot, or shut down operation by a specified number of minutes, 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:

To schedule the halt, reboot, or shutdown at a specific 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 and juniper_junos_system modules perform the requested operation on all Routing Engines in a dual Routing Engine or Virtual Chassis setup. You can also instruct the modules 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 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 and juniper_junos_system modules support the vmhost argument, which enables you to reboot 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.

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 system module in the juniper.device collection 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 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 Checking NETCONF connectivity task, which utilizes the 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

Creating and Executing 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.

Release History Table
Release
Description
2.0.0
Starting in Juniper.junos Release 2.0.0, the juniper_junos_system module replaces the functionality of the junos_shutdown and junos_zeroize modules.