You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Rundeck中根据节点OS类型执行不同的重启命令?

针对不同操作系统节点执行Rundeck重启作业的解决方案

核心问题分析

直接在作业中同时执行sudo reboot和shutdown /r /t 0会导致每个节点都运行两条不匹配自身系统的命令,引发报错。必须按节点的操作系统属性过滤,让对应节点只执行适配的重启命令。

实现方案:按节点元数据/标签过滤执行步骤

Rundeck完全支持针对选中节点的子集执行步骤,以下是两种可靠实现方式:

1. 步骤级别添加节点过滤器

在每个Command步骤中配置节点过滤规则,仅匹配对应操作系统的节点:

  • Linux节点步骤:过滤osFamily=unix(Rundeck自动识别的系统属性)或自定义标签如linux的节点
  • Windows节点步骤:过滤osFamily=windows或自定义标签如windows的节点

修改后的作业XML示例:

<joblist>
  <job>
    <defaultTab>nodes</defaultTab>
    <description>Restarts the selected target hosts</description>
    <dispatch>
      <excludePrecedence>true</excludePrecedence>
      <keepgoing>true</keepgoing>
      <rankOrder>ascending</rankOrder>
      <successOnEmptyNodeFilter>false</successOnEmptyNodeFilter>
      <threadcount>1</threadcount>
    </dispatch>
    <executionEnabled>true</executionEnabled>
    <group>Service</group>
    <id>6d757927-6879-4375-91da-dbd0fcf79c79</id>
    <loglevel>INFO</loglevel>
    <name>Restart Hosts</name>
    <nodeFilterEditable>false</nodeFilterEditable>
    <nodefilters>
      <filter></filter>
    </nodefilters>
    <nodesSelectedByDefault>false</nodesSelectedByDefault>
    <plugins />
    <scheduleEnabled>true</scheduleEnabled>
    <sequence keepgoing='true' strategy='parallel'>
      <!-- Linux节点专属重启步骤 -->
      <command>
        <exec>sudo reboot</exec>
        <nodefilters>
          <filter>osFamily:unix</filter>
        </nodefilters>
      </command>
      <!-- Windows节点专属重启步骤 -->
      <command>
        <exec>shutdown /r /t 0</exec>
        <nodefilters>
          <filter>osFamily:windows</filter>
        </nodefilters>
      </command>
    </sequence>
    <uuid>6d757927-6879-4375-91da-dbd0fcf79c79</uuid>
  </job>
</joblist>

说明:

  • 若使用自定义标签,过滤器可改为tags:linux或tags:windows
  • keepgoing='true'保证某类节点执行失败时,另一类节点的重启操作不受影响

2. 单步骤动态判断操作系统

使用Script步骤,通过Rundeck节点属性变量自动判断系统类型,执行对应命令:

Linux/Bash版本脚本

if [ "${node.osFamily}" = "unix" ]; then
  sudo reboot
elif [ "${node.osFamily}" = "windows" ]; then
  shutdown /r /t 0
fi

Windows/PowerShell版本脚本

if ($env:RD_NODE_OSFAMILY -eq "windows") {
  shutdown /r /t 0
} elseif ($env:RD_NODE_OSFAMILY -eq "unix") {
  sudo reboot
}

这种方式无需拆分多个步骤,一个步骤即可兼容两类系统,更简洁。

注意事项

  • 确认节点的osFamily属性配置正确,可在Rundeck节点管理页面查看节点详情
  • Linux节点需确保执行用户拥有sudo reboot的免密权限(可通过sudoers文件配置)
  • Windows节点需确保执行用户具备系统重启权限

内容的提问来源于stack exchange,提问作者MOnsDaR

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 05:03:28