Rundeck-根据输入选项动态运行步骤

时间:2020-04-22 08:25:12

标签: rundeck

我想根据不同的输入 Options 输入个步骤

例如,我有一个 Job ,它以{em> year 作为输入参数运行./script.sh

./script.sh <year>

这个想法是将“年”的列表设置为“ 选项”参数(类似2018, 2019, 2020),并每年(每年){{1} }应该作为单个 Step 运行。

  • step_1:script.sh
  • step_2:script.sh <year>
  • step_3:./script.sh 2018
  • step_n:./script.sh 2019

有办法吗?

谢谢。

更新2020-04-22

在Rundeck上预期,每个 Command 都有一年: enter image description here

1 个答案:

答案 0 :(得分:0)

我做了这个示例,使用了一个选项,将其传递给使用@option.myoption@进行迭代的内联脚本(并将“外部”脚本步骤作为参数引用)。

<joblist>
  <job>
    <context>
      <options preserveOrder='true'>
        <option name='theyear' value='2010,2011,2012,2013,2014,2015,2016,2017,2018,2019,2020' />
      </options>
    </context>
    <defaultTab>nodes</defaultTab>
    <description></description>
    <executionEnabled>true</executionEnabled>
    <id>a72d4d32-d4fe-4774-b18c-723d18842c5e</id>
    <loglevel>INFO</loglevel>
    <name>WhatYearIs</name>
    <nodeFilterEditable>false</nodeFilterEditable>
    <plugins />
    <scheduleEnabled>true</scheduleEnabled>
    <sequence keepgoing='false' strategy='node-first'>
      <command>
        <fileExtension>.sh</fileExtension>
        <script><![CDATA[#!/bin/bash
DataList=" @option.theyear@ "
Field_Separator=$IFS

# set comma as internal field separator for the string list
IFS=,
for val in $DataList;
    do
        # echo $val
        bash /path/to/your/script/year.sh $val
    done

IFS=$Field_Separator]]></script>
        <scriptargs />
        <scriptinterpreter>/bin/bash</scriptinterpreter>
      </command>
    </sequence>
    <uuid>a72d4d32-d4fe-4774-b18c-723d18842c5e</uuid>
  </job>
</joblist>

要测试的bash脚本:

#!/bin/bash
# print_args.sh
echo "Current year is:" "$@"

# You could pass all arguments to another program like this
# myProgram "$@"

更新:您不能在运行时动态地添加作业的步骤,但是,另一种方法基本上是对第一个建议的修改,不同之处在于我们通过{{ 3}}内联脚本上的工具,该工具调用(并通过年份作为选项)以单独执行。

工作年限(作为父级工作)。

<joblist>
  <job>
    <context>
      <options preserveOrder='true'>
        <option name='theyear' value='2018,2019,2020' />
      </options>
    </context>
    <defaultTab>nodes</defaultTab>
    <description></description>
    <executionEnabled>true</executionEnabled>
    <group>SO</group>
    <id>a72d4d32-d4fe-4774-b18c-723d18842c5e</id>
    <loglevel>INFO</loglevel>
    <name>WhatYearIs</name>
    <nodeFilterEditable>false</nodeFilterEditable>
    <plugins />
    <scheduleEnabled>true</scheduleEnabled>
    <sequence keepgoing='false' strategy='node-first'>
      <command>
        <script><![CDATA[#!/bin/bash
DataList=" @option.theyear@ "
Field_Separator=$IFS

# set comma as internal field separator for the string list
IFS=,
for val in $DataList;
    do
        # echo $val
        sleep 3
        rd run -p ProjectEXAMPLE -j WhatYearChild -- -childyear $val
    done

IFS=$Field_Separator]]></script>
        <scriptargs />
      </command>
    </sequence>
    <uuid>a72d4d32-d4fe-4774-b18c-723d18842c5e</uuid>
  </job>
</joblist>

WhatYearChild(作为执行脚本的子作业)。

<joblist>
  <job>
    <context>
      <options preserveOrder='true'>
        <option name='childyear' />
      </options>
    </context>
    <defaultTab>nodes</defaultTab>
    <description></description>
    <executionEnabled>true</executionEnabled>
    <group>SO</group>
    <id>76c15c6d-be45-4540-8f44-8823ed2d2483</id>
    <loglevel>INFO</loglevel>
    <name>WhatYearChild</name>
    <nodeFilterEditable>false</nodeFilterEditable>
    <plugins />
    <scheduleEnabled>true</scheduleEnabled>
    <sequence keepgoing='false' strategy='node-first'>
      <command>
        <script><![CDATA[echo "Current year is: @option.childyear@"]]></script>
        <scriptargs />
      </command>
    </sequence>
    <uuid>76c15c6d-be45-4540-8f44-8823ed2d2483</uuid>
  </job>
</joblist>

获得rd-cli结果(从技术上讲,这是每次迭代的单个步骤)。