bash Rundeck 跨作业步骤共享变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37110172/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 14:36:59  来源:igfitidea点击:

Rundeck sharing variables across job steps

bashvariablesrundeck

提问by Naveen Karnam

I want to share a variable across rundeck job steps.

我想在 rundeck 作业步骤中共享一个变量。

  1. Initialized a job option "target_files"
  2. Set the variable on STEP 1.

    RD_OPTION_TARGET_FILES=some bash command
    echo $RD_OPTION_TARGET_FILES
    The value is printed here.

  3. Read the variable from STEP 2.
    echo $RD_OPTION_TARGET_FILES

  1. 初始化作业选项“target_files”
  2. 在步骤 1 中设置变量。

    RD_OPTION_TARGET_FILES=some bash command
    echo $RD_OPTION_TARGET_FILES
    该值打印在此处。

  3. 读取步骤 2 中的变量。
    echo $RD_OPTION_TARGET_FILES

Step 3 doesn't recognize the variable set in STEP 1.
What's a good way of doing this on rundeck other than using environment variables?

第 3 步无法识别在第 1 步中设置的变量。
除了使用环境变量之外,在 rundeck 上执行此操作的好方法是什么?

回答by Happy

After Rundeck 2.9, there is a Data Capture Pluginto allow pass data between job steps.

在 Rundeck 2.9 之后,有一个Data Capture Plugin允许在作业步骤之间传递数据。

The plugin is contained in the Rundeck application by default.

默认情况下,该插件包含在 Rundeck 应用程序中。

Data capture plugin to match a regular expression in a step's log output and pass the values to later steps

数据捕获插件,用于匹配步骤日志输出中的正则表达式并将值传递给后续步骤

Details see Data Capture/Data Passing between steps(Published: 03 Aug 2017)

详细信息请参见步骤之间的数据捕获/数据传递(发布时间:2017 年 8 月 3 日)

回答by berhauz

The detailed procedure from RUNDECK 2.9+:

RUNDECK 2.9+ 的详细过程:

1) set the values - three methods:

1) 设置值 - 三种方法:

1.a) use a "global variable" workflow step type e.g. fill in: Group:="export", Name:="varOne", Value:="hello"

1.a) 使用“全局变量”工作流程步骤类型,例如填写:Group:="export", Name:="varOne", Value:="hello"

1.b) add to the workflow a "global log filter" (the Data Capture Plugin cited by 'Amos' here) which takes a regular expression that is evaluated on job step log outputs. For instance with a job step command like:

1.b) 向工作流程添加一个“全局日志过滤器”(此处由“Amos”引用的数据捕获插件),它采用在作业步骤日志输出上进行评估的正则表达式。例如,使用如下作业步骤命令:

   echo "CaptureThis:varTwo=world"

and a global log filter pattern like:

以及全局日志过滤器模式,例如:

   "CaptureThis:(.*?)=(.*)" 

('Name Data' field not needed unless you supply a single capturing group in the pattern)

(除非您在模式中提供单个捕获组,否则不需要“名称数据”字段)

1.c) use a workflow Data Step to define multiple variables explicitly. Example contents:

1.c) 使用工作流数据步骤明确定义多个变量。示例内容:

varThree=foo
varFour=bar

2) get the values back:

2)取回值:

you must use the syntax ${ctx.name} in command strings and args, and @ctx.name@ within INLINE scripts. In our example, with a job step command or inline script line like:

您必须在命令字符串和参数中使用语法 ${ctx.name},在内联脚本中使用 @ctx.name@。在我们的示例中,使用作业步骤命令或内联脚本行,例如:

echo "values : @export.varOne@, @data.varTwo@, @stub.varThree@, @stub.varFour@"

you'll echo the four values.

你会回应这四个值。

The context is implicitly 'data' for method 1.b and 'stub' for method 1.c.

上下文是方法 1.b 的隐式“数据”和方法 1.c 的“存根”。

Note that a data step is quite limitative! It only allows to benefit from @stub.name@ notations within inline scripts. Value substitution is not performed in remote files, and notations like ${stub.name} are not available in job step command strings or arguments.

请注意,数据步骤非常有限!它只允许从内联脚本中的 @stub.name@ 符号中受益。远程文件中不执行值替换,并且作业步骤命令字符串或参数中不提供 ${stub.name} 等符号。

回答by Leo Prince

Nearly there are no ways in job Inline scripts other than 1, exporting the value to env or 2, writing the value to a 3rd file at step1 and step2 reading it from there.

除了 1,在作业内联脚本中几乎没有其他方法,将值导出到 env 或 2,在 step1 和 step2 将值写入第三个文件,然后从那里读取它。

If you are using "Scriptfile or URL" method, may be you can execute step2 script with in script1 as a work around.. like

如果您使用的是“Scriptfile 或 URL”方法,您可能可以在 script1 中执行 step2 脚本作为解决方法。

Script1
#!/bin/bash
. ./script2

In the above case script2 will execute in the same session as script1 so the variables and values are preserved.

在上述情况下,script2 将在与 script1 相同的会话中执行,因此变量和值被保留。

EDITEarlier there was no such options but later there are available plugins. Hence check Amos'sanswer.

编辑早些时候没有这样的选项,但后来有可用的插件。因此检查阿莫斯的答案。