如何在Rundeck中编程实现步骤间数据传递?含Java插件场景
在Rundeck NodeStepPlugin间编程传递数据的可行方案
方法1:正确使用SharedDataContext(跨步骤/节点共享)
NodeStepPlugin默认支持通过SharedDataContext实现数据传递,核心是注意作用域选择:跨节点的步骤要使用execution作用域,而非节点专属的node作用域。
GeneratorPlugin 代码示例(存储数据)
@Override public void execute(NodeStepContext context, Map<String, Object> configuration) throws NodeStepException { // 生成业务变量 String generatedValue = "plugin-generated-data"; // 获取全局共享上下文 SharedDataContext sharedData = context.getExecutionContext().getSharedDataContext(); // 存入execution作用域(所有节点步骤均可访问) sharedData.put("execution", "customGeneratedVar", generatedValue); }
ConsumerPlugin 代码示例(读取数据)
@Override public void execute(NodeStepContext context, Map<String, Object> configuration) throws NodeStepException { SharedDataContext sharedData = context.getExecutionContext().getSharedDataContext(); // 读取execution作用域的变量 String targetValue = sharedData.get("execution", "customGeneratedVar"); if (targetValue != null) { // 执行后续业务逻辑 } }
方法2:利用插件输出属性(标准插件交互方式)
通过定义插件的输出属性,将数据写入Rundeck的输出上下文,后续步骤可通过数据上下文直接读取。
GeneratorPlugin 定义输出属性+存储数据
// 先定义输出属性 @Override public List<Attribute> getOutputAttributes() { return Arrays.asList( new Attribute("generatedVar", String.class) ); } // 在execute方法中写入输出 @Override public void execute(NodeStepContext context, Map<String, Object> configuration) throws NodeStepException { String generatedValue = "plugin-output-data"; // 写入输出上下文,第一个参数为插件步骤的标识名(需在工作流中配置该步骤的名称) context.getOutputContext().addOutput("generatorStep", "generatedVar", generatedValue); }
ConsumerPlugin 读取输出属性
@Override public void execute(NodeStepContext context, Map<String, Object> configuration) throws NodeStepException { DataContext dataContext = context.getDataContext(); // 通过steps.步骤名.属性名读取 String targetValue = dataContext.get("steps", "generatorStep.generatedVar"); // 或使用插值器直接解析变量(需确保插值功能启用) // String targetValue = context.getInterpolator().interpolate("${steps.generatorStep.generatedVar}"); }
注意:需在Rundeck工作流配置中,给GeneratorPlugin的步骤设置名称为
generatorStep(与代码中对应)。
方法3:设置工作流变量(全局可访问)
通过Rundeck的ExecutionService直接设置工作流全局变量,所有后续步骤(包括其他插件)均可读取。
GeneratorPlugin 设置工作流变量
@Override public void execute(NodeStepContext context, Map<String, Object> configuration) throws NodeStepException { String generatedValue = "workflow-global-data"; // 获取ExecutionService实例 ExecutionService executionService = context.getExecutionContext().getServices().get(ExecutionService.class); // 设置全局工作流变量 executionService.setWorkflowVariable(context.getExecutionContext(), "globalGeneratedVar", generatedValue); }
ConsumerPlugin 读取工作流变量
@Override public void execute(NodeStepContext context, Map<String, Object> configuration) throws NodeStepException { // 从数据上下文读取工作流变量 String targetValue = context.getDataContext().get("workflow", "globalGeneratedVar"); // 或使用插值器解析 // String targetValue = context.getInterpolator().interpolate("${workflow.globalGeneratedVar}"); }
常见问题排查
- 作用域错误:如果两个插件在不同节点执行,使用
node作用域的共享数据会无法跨节点传递,必须切换为execution作用域。 - 版本兼容性:确保Rundeck版本与插件API兼容,
SharedDataContext在Rundeck 3.0+版本才稳定支持。 - 日志验证:在两个插件的
execute方法中添加日志,确认数据是否成功存入、是否能正常读取。
内容的提问来源于stack exchange,提问作者Shatazone
相关产品推荐
相关产品推荐

