Help us improve your experience.

Let us know what you think.

Do you have time for a two-minute survey?

Using Ansible to Halt, Reboot, or Shut Down Devices Running Junos OS

 

Using Ansible to Halt, Reboot, or Shut Down Devices

The juniper_junos_system module in the Juniper.junos role enables you to halt, reboot, or shut down devices running Junos OS. You can perform an immediate system halt, reboot, or shutdown operation, request the operation with an optional delay, or you can schedule the operation at a specified date and time. By default, the juniper_junos_system module immediately executes the requested operation and performs the operation on all Routing Engines, if in a dual Routing Engine or Virtual Chassis setup.

Note

Starting in Ansible for Junos OS Release 2.0.0, the juniper_junos_system module replaces the functionality of the junos_shutdown and junos_zeroize modules.

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

Table 1: 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 juniper_junos_system module with action: "reboot" to immediately reboot all Routing Engines on the hosts in the specified inventory group.

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:

By default the juniper_junos_system module halts, reboots, or shuts down 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 other Routing Engines except the one to which the application is connected.

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 other Routing Engines in the system but not on the Routing Engine to which the application is connected, include the other_re: true argument.

Table 2 summarizes the all_re and other_re values that are required to execute the requested operation on specific Routing Engines.

Table 2: juniper_junos_system Parameters for Specifying Routing Engines

Affected Routing Engines

all_re Parameter

other_re Parameter

All Routing Engines (default)

Omitted or set to true

Omitted

Only the connected Routing Engine

Set to false

Omitted

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

Omitted

Set to true

Example: Using Ansible to Reboot Devices

Juniper Networks provides support for using Ansible to manage devices running Junos OS. This example outlines how to use Ansible to reboot a device running Junos OS.

Requirements

This example uses the following hardware and software components:

  • Configuration management server running Ansible 2.1 or later with version 2.0.0 or later of the Juniper.junos role installed

  • Device running Junos OS with NETCONF enabled and a user account configured with appropriate permissions

  • SSH public/private key pair configured for the appropriate user on the Ansible server and device running Junos OS

  • Existing Ansible inventory file with required hosts defined

Overview

The juniper_junos_system module in the Juniper.junos role enables you to halt, reboot, or shut down devices running Junos OS. The value of the module’s, action argument defines the operation to execute on the host. This example presents an Ansible playbook that uses the juniper_junos_system module to reboot a device running Junos OS.

Note

Starting in Ansible for Junos OS Release 2.0.0, the juniper_junos_system module replaces the functionality of the junos_shutdown and junos_zeroize modules.

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 affected 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 machine skips the other tasks in the play for that device.

This playbook includes the task Checking NETCONF connectivity, which utilizes the Ansible module wait_for to try to establish a NETCONF session with the device running Junos OS using the NETCONF default port 830. If the control machine fails to establish a NETCONF session with the device during playbook execution, it skips the remaining tasks in the play for that device.

The task to reboot the device executes the juniper_junos_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. To halt or shut down the device, set the action argument to "halt" or "shutdown", respectively. 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 machine attempts to reconnect with the device.

Configuration

Creating and Executing the Ansible Playbook

Step-by-Step Procedure

To create a playbook that uses the juniper_junos_system module to reboot a device running Junos OS:

  1. Include the boilerplate for the playbook and this play, which must contain connection: local and the Juniper.junos role.

  2. Define or import any necessary variables.

  3. Create an interactive prompt to prevent the accidental execution of the module.

  4. Create the task that confirms the users intent.

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

  6. Create the task to reboot the device after the specified number of minutes.

  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 installation task.

Results

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

Executing the Playbook

Step-by-Step Procedure

To execute the playbook:

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

    user@ansible-cm:~/ansible$ ansible-playbook ansible-pb-junos-reboot.yaml

Verification

Verifying the Reboot

Purpose

Verify that the device running Junos OS successfully rebooted.

Action

When you execute the playbook, review the execution output of the wait_reboot task.

Meaning

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

Release History Table
Release
Description
Starting in Ansible for Junos OS Release 2.0.0, the juniper_junos_system module replaces the functionality of the junos_shutdown and junos_zeroize modules.