使用 juniper.device.system Ansible 模块停止、重新启动或关闭 Junos 设备
使用 juniper.device.system Ansible 模块停止、重新启动或关闭运行 Junos OS 的设备或运行 Junos OS Evolved 的设备。
使用 Ansible 停止、重启或关闭设备
瞻博网络提供了一个 Ansible 模块,您可以使用该模块停止、重新启动或关闭运行 Junos OS 的设备或运行 Junos OS Evolved 的设备。 表 1 概述了可用的模块。
|
收藏 |
模块集 |
模块名称 |
|---|---|---|
|
|
您可以使用该 juniper.device.system 模块请求对 Junos 设备执行以下操作。默认情况下,模块会立即执行请求的操作,并在双路由引擎或虚拟机箱设置中的所有路由引擎上执行该操作。
-
系统立即停止、重新启动或关闭
-
停止、重新启动或关闭操作,提供可选延迟
-
计划在特定日期和时间执行的停止、重启或关闭操作
该 system 模块需要一个参数, action用于定义要执行的操作。 表 2 定义了 action 参数值,并简要介绍了每个操作以及相应的 CLI 命令。有关该 "zeroize" 操作的信息,请参阅 使用 juniper.device.system Ansible 模块将 Junos 设备恢复到出厂默认状态。
|
|
描述 |
等效 CLI 命令 |
|---|---|---|
|
|
正常关闭 Junos OS 软件或 Junos OS Evolved 软件,但保留系统电源 |
|
|
|
重新启动 Junos OS 软件或 Junos OS Evolved 软件 |
|
|
|
正常关闭 Junos OS 软件或 Junos OS 演化版软件,然后关闭路由引擎 |
|
以下 Ansible 操作指南使用 juniper.device.system 模块 with action: reboot 立即重新启动指定清单组中主机上的所有路由引擎。
---
- name: Reboot Junos devices
hosts: dc1
connection: local
gather_facts: no
tasks:
- name: Reboot all REs on the device
juniper.device.system:
action: reboot
如何在指定时间执行延迟、重启或关机
您可以将停止、重新启动或关闭操作延迟指定的分钟数。要添加延迟,请将可选 in_min 参数设置为系统在执行操作之前应等待的分钟数。以下任务请求在 30 分钟内重新启动所有路由引擎:
---
- name: Reboot Junos devices
hosts: dc1
connection: local
gather_facts: no
tasks:
- name: Reboot all REs in 30 minutes
juniper.device.system:
action: reboot
in_min: 30
您还可以将停止、重新启动或关闭操作安排在特定时间。要安排时间,请包含 at 参数,该参数采用可通过以下方式之一指定的字符串:
-
now— 立即启动软件的停止、重新启动或关闭。 -
+minutes— 从现在开始启动请求的操作的分钟数。 -
yymmddhhmm- 启动请求操作的绝对时间,指定为年、月、日、小时和分钟。 -
hh:mm- 当天启动请求操作的绝对时间,以 24 小时为单位指定。
以下任务计划在当天 22:30 关闭所有路由引擎:
tasks:
- name: Shut down all REs at 22:30 on the current day
juniper.device.system:
action: shutdown
at: "22:30"
如何指定目标路由引擎
默认情况下,模块 juniper.device.system 会在双路由引擎或虚拟机箱设置中的所有路由引擎上执行所请求的路由引擎操作。您还可以指示模块仅在应用程序连接的路由引擎上执行操作,或在除应用程序连接到的路由引擎之外的所有路由引擎上执行操作。
要指定路由引擎,请使用 all_re 和 other_re 参数。 表 3 总结了 all_re 在特定路由引擎上执行所请求操作所需的 和 other_re 值。
|
受影响的路由引擎 |
|
|
|---|---|---|
|
所有路由引擎(默认) |
省略或设置为 |
– |
|
仅连接的路由引擎 |
设置为 |
– |
|
除应用连接的路由引擎外的所有路由引擎 |
– |
设置为 |
要显式指示应在双路由引擎或虚拟机箱设置中的所有路由引擎上执行路由引擎,请包含 all_re: true 参数,这是默认值。
---
- name: Reboot Junos devices
hosts: dc1
connection: local
gather_facts: no
tasks:
- name: Reboot all Routing Engines
juniper.device.system:
action: reboot
all_re: true
要仅在应用程序连接到的路由引擎上执行请求的操作,请包含 all_re: false 参数。
tasks:
- name: Reboot only the connected Routing Engine
juniper.device.system:
action: reboot
all_re: false
要对系统中的所有路由引擎(应用程序连接到的路由引擎除外)执行请求的操作,请包含 other_re: true 参数。
tasks:
- name: Shut down all other Routing Engines
juniper.device.system:
action: shutdown
other_re: true
如何重新启动或关闭虚拟机主机
在具有支持 VM 主机的路由引擎的设备上,Junos OS 在基于 Linux 的主机(VM 主机)上作为虚拟机 (VM) 运行。该 juniper.device.system 模块支持该 vmhost 参数,使您能够重新启动或关闭虚拟机主机。
包含 和action: rebootvmhost: true参数后,系统将通过执行<request-vmhost-reboot>与操作模式命令对应request vmhost reboot的 RPC 重新启动所有路由引擎上的主机操作系统和兼容的 Junos OS。
同样,当您包含 和action: shutdownvmhost: true参数时,系统将通过执行<request-vmhost-poweroff>与操作模式命令对应request vmhost power-off的 RPC 来关闭所有路由引擎上的主机操作系统和兼容的 Junos OS。
以下操作指南会执行虚拟机主机重新启动,这会同时重新启动主机操作系统和来宾 Junos OS。
---
- name: Reboot VM Hosts
hosts: vm_hosts
connection: local
gather_facts: no
tasks:
- name: Reboot VM host
juniper.device.system:
action: reboot
vmhost: true
all_re: false
示例:使用 Ansible 重新启动 Junos 设备
该 juniper.device.system 模块允许您停止、重新启动或关闭 Junos 设备。此示例使用该 system 模块重新启动 Junos 设备。
要求
此示例使用以下硬件和软件组件:
-
运行 Ansible 2.17 或更高版本并安装集合的
juniper.device配置管理服务器 -
启用了 NETCONF 且用户帐户配置了适当权限的 Junos 设备
-
在 Ansible 控制节点和 Junos 设备上为相应用户配置的 SSH 公钥/私钥对
-
定义了所需主机的现有 Ansible 清单文件
概述
此示例展示了一个 Ansible 操作指南,该操作指南使用该 juniper.device.system 模块重新启动 Junos 设备。模块参数 action 的值定义要在主机上执行的操作。
从剧本调用模块时,建议使用交互式提示来确认用户是否打算重新启动指定的设备。如果用户无意中运行了操作指南,并且没有进行检查,则可能会对需要受影响设备的任何网络产生不利影响。作为预防措施,此操作指南使用交互式提示来验证用户是否打算重新启动设备。提示符要求用户在命令行上手动键入“'yes'才能执行该模块。如果 Confirmation check 任务失败,Ansible 控制节点将跳过该设备正在执行的其他任务。
此操作指南包含任务 Check NETCONF connectivity ,该任务利用模块尝试 ansible.builtin.wait_for 使用默认 NETCONF 端口 830 与 Junos 设备建立 NETCONF 会话。如果控制节点在操作指南执行期间未能与设备建立 NETCONF 会话,则将跳过该设备正在执行的剩余任务。
如果确认和 NETCONF 检查成功,则重新启动任务将执行模块 juniper.device.system 。该 action 参数设置为 reboot,表示应重新启动软件。该 in_min: 2 参数指示模块在执行重新启动命令之前等待指定的分钟数。这为任何用户注销系统提供了时间。
该任务将模块结果存储在变量中 result ,并通知两个处理程序。 pause_for_reboot 启动重新启动操作后,处理程序会等待指定的时间,以防止 wait_reboot 处理程序在重新启动之前错误地检测到设备处于联机状态。然后, wait_reboot 处理程序尝试与设备建立会话,以验证设备在重新启动后是否重新联机。该 wait_time_after_reboot 变量定义控制节点尝试与设备重新连接的时间长度。
配置
创建并执行 Ansible 剧本
分步程序
要创建使用该 juniper.device.system 模块重新启动 Junos 设备的操作指南:
-
包括 playbook 和 play(在本地执行模块)的样板。
--- - name: Reboot Junos devices hosts: dc1 connection: local gather_facts: no
-
定义或导入任何必要的变量。
vars: wait_time_after_reboot: 300 netconf_port: 830 -
创建交互式提示,以防止用户在未首先了解其含义的情况下意外执行该模块。
vars_prompt: - name: "reboot_confirmation" prompt: "This playbook reboots devices. Enter 'yes' to continue" private: no -
创建确认用户意图的任务。
tasks: - name: Confirmation check fail: msg="Playbook run confirmation failed" when: reboot_confirmation != "yes" -
(选答)创建任务以验证 NETCONF 连接。
- name: Check NETCONF connectivity ansible.builtin.wait_for: host: "{{ inventory_hostname }}" port: "{{ netconf_port }}" timeout: 5 -
创建任务以在指定的分钟数后重新启动设备,然后通知处理程序。
- name: Reboot all Routing Engines on the Junos device juniper.device.system: action: reboot in_min: 2 all_re: true register: result notify: - pause_for_reboot - wait_reboot -
(选答)创建任务以打印响应。
- name: Print response ansible.builtin.debug: var: result -
创建重新启动后暂停的处理程序和验证设备在重新启动后是否重新联机的处理程序。
处理程序名称应与重新启动任务中引用的名称相同。
handlers: - name: pause_for_reboot pause: seconds: 180 when: result.reboot - name: wait_reboot ansible.builtin.wait_for: host: "{{ inventory_hostname }}" port: "{{ netconf_port }}" timeout: "{{ wait_time_after_reboot }}" when: result.reboot
结果
在 Ansible 控制节点上,查看完成的操作指南。如果 playbook 未显示预期的代码,请重复此示例中的说明以更正 playbook。
---
- name: Reboot Junos devices
hosts: dc1
connection: local
gather_facts: no
vars:
wait_time_after_reboot: 300
netconf_port: 830
vars_prompt:
- name: "reboot_confirmation"
prompt: "This playbook reboots devices. Enter 'yes' to continue"
private: no
tasks:
- name: Confirmation check
fail: msg="Playbook run confirmation failed"
when: reboot_confirmation != "yes"
- name: Check NETCONF connectivity
ansible.builtin.wait_for:
host: "{{ inventory_hostname }}"
port: "{{ netconf_port }}"
timeout: 5
- name: Reboot all Routing Engines on the Junos device
juniper.device.system:
action: reboot
in_min: 2
all_re: true
register: result
notify:
- pause_for_reboot
- wait_reboot
- name: Print response
ansible.builtin.debug:
var: result
handlers:
- name: pause_for_reboot
pause:
seconds: 180
when: result.reboot
- name: wait_reboot
ansible.builtin.wait_for:
host: "{{ inventory_hostname }}"
port: "{{ netconf_port }}"
timeout: "{{ wait_time_after_reboot }}"
when: result.reboot
执行剧本
过程
分步程序
要执行该剧本,请执行以下步骤:
-
在控制节点上发出
ansible-playbook命令,并提供 playbook 路径和任何所需的选项。user@ansible-cn:~/ansible$ ansible-playbook ansible-pb-junos-reboot.yaml This playbook reboots devices. Enter 'yes' to continue: yes PLAY [Reboot Junos devices] ************************************************** TASK [Confirmation check] **************************************************** skipping: [dc1a.example.net] TASK [Check NETCONF connectivity] ******************************************** ok: [dc1a.example.net] TASK [Reboot all Routing Engines on the Junos device] ************* changed: [dc1a.example.net] TASK [Print response] ******************************************************** ok: [dc1a.example.net] => { "result": { "action": "reboot", "all_re": true, "changed": true, "failed": false, "media": false, "msg": "reboot successfully initiated. Response got Shutdown at Fri Dec 11 17:36:50 2020. [pid 11595]", "other_re": false, "reboot": true, "vmhost": false } } RUNNING HANDLER [pause_for_reboot] ******************************************* Pausing for 180 seconds (ctrl+C then 'C' = continue early, ctrl+C then 'A' = abort) ok: [dc1a.example.net] RUNNING HANDLER [wait_reboot] ************************************************ ok: [dc1a.example.net] PLAY RECAP ******************************************************************* dc1a.example.net : ok=5 changed=1 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
变更历史表
是否支持某项功能取决于您使用的平台和版本。使用 功能资源管理器 确定您的平台是否支持某个功能。
juniper.device ,该 system 模块支持关闭虚拟机主机。