【问题标题】:Rundeck pass common parameters/variable to the flowRundeck 将常用参数/变量传递给流
【发布时间】:2021-03-28 19:34:31
【问题描述】:

假设我有一个主要的流程执行工作,我将来还会有更多这样的工作。

我将有很少的后端流程作业将用作我上面提到的主要流程执行作业中的参考作业。

我需要完成的事情:

  1. 在主要流程作业中传递一些命令行参数以供以后参考作业使用
  2. 此外,如果参考作业会生成更多变量,这些变量应该可供以后的参考步骤使用。

我在 Rundeck 网站上没有找到包含示例的非常清晰的文档,如果有人能提供帮助,我将不胜感激。

#Update:

在将列表的文本选项设置为纯文本后,我可以在我添加的任何新步骤中使用它,例如内联脚本或命令。

但是当我想在工作流步骤中的作业参考中使用相同的选项时,即使在传递了命令行参数之后,我也可以在我的 python 脚本中得到它。

我双向传递了参数,但无法在参考作业中获取选项的值。像这样提到:-source_details ${option.source_details}

也喜欢这个${option.source_details}

我在这里提到的子作业正在调用一个 python 脚本,而我正在使用 sys.argv 检查命令行参数,除了实际的 python 文件名之外没有提供任何额外的东西。

如果我在这里遗漏了什么,请纠正我。

【问题讨论】:

    标签: rundeck


    【解决方案1】:

    为此,您需要将选项作为参数传递,例如,子作业使用自己的选项作为参数。

    我举个例子:

    <joblist>
      <job>
        <context>
          <options preserveOrder='true'>
            <option name='age' value='32' />
            <option name='name' value='Alice' />
          </options>
        </context>
        <defaultTab>nodes</defaultTab>
        <description></description>
        <executionEnabled>true</executionEnabled>
        <id>bf45b9bd-f8f4-4f00-8aaf-86572b637e05</id>
        <loglevel>INFO</loglevel>
        <name>ChildJob</name>
        <nodeFilterEditable>false</nodeFilterEditable>
        <plugins />
        <scheduleEnabled>true</scheduleEnabled>
        <sequence keepgoing='false' strategy='node-first'>
          <command>
            <fileExtension>.sh</fileExtension>
            <script><![CDATA[echo "Username: @option.name@";
    echo "Age: @option.age@";]]></script>
            <scriptargs />
            <scriptinterpreter>/bin/bash</scriptinterpreter>
          </command>
        </sequence>
        <uuid>bf45b9bd-f8f4-4f00-8aaf-86572b637e05</uuid>
      </job>
    </joblist>
    

    并且父作业可以传递另一个选项来覆盖子作业选项:

    <joblist>
      <job>
        <context>
          <options preserveOrder='true'>
            <option name='age' value='25' />
            <option name='name' value='Bob' />
          </options>
        </context>
        <defaultTab>nodes</defaultTab>
        <description></description>
        <executionEnabled>true</executionEnabled>
        <id>9fa68cd8-5bb0-4341-be32-f58c372cb765</id>
        <loglevel>INFO</loglevel>
        <name>Parent</name>
        <nodeFilterEditable>false</nodeFilterEditable>
        <plugins />
        <scheduleEnabled>true</scheduleEnabled>
        <sequence keepgoing='false' strategy='node-first'>
          <command>
            <jobref name='ChildJob' nodeStep='true'>
              <arg line='-name ${option.name} -age ${option.age}' />
              <uuid>bf45b9bd-f8f4-4f00-8aaf-86572b637e05</uuid>
            </jobref>
          </command>
        </sequence>
        <uuid>9fa68cd8-5bb0-4341-be32-f58c372cb765</uuid>
      </job>
    </joblist>
    

    简而言之:您可以在工作中使用选项(从父工作“接收”或使用自身),并在整个工作流程中保持您的价值观。

    使用 python3 示例更新

    概念是一样的,在子作业中你可以创建一些选项来接收父作业的值,看看。

    子工作:

    <joblist>
      <job>
        <context>
          <options preserveOrder='true'>
            <option name='arg1' value='one' />
            <option name='arg2' value='two' />
            <option name='arg3' value='three' />
          </options>
        </context>
        <defaultTab>nodes</defaultTab>
        <description></description>
        <executionEnabled>true</executionEnabled>
        <id>159e14d6-29e2-4fe9-b9b3-b1621d59843d</id>
        <loglevel>INFO</loglevel>
        <name>PythonChildJob</name>
        <nodeFilterEditable>false</nodeFilterEditable>
        <plugins />
        <scheduleEnabled>true</scheduleEnabled>
        <sequence keepgoing='false' strategy='node-first'>
          <command>
            <fileExtension>.py</fileExtension>
            <script><![CDATA[#!/usr/bin/python3
    
    import sys
    
    print ('Number of arguments:', len(sys.argv), 'arguments.')
    print ('Argument List:', str(sys.argv))]]></script>
            <scriptargs>${option.arg1} ${option.arg2} ${option.arg3}</scriptargs>
            <scriptinterpreter>/usr/bin/python3.8</scriptinterpreter>
          </command>
        </sequence>
        <uuid>159e14d6-29e2-4fe9-b9b3-b1621d59843d</uuid>
      </job>
    </joblist>
    

    而父作业,你可以传递选项或只是像-arg1 hello -arg2 from -arg3 mars这样的字符串

    <joblist>
      <job>
        <context>
          <options preserveOrder='true'>
            <option name='opt1' value='hello' />
            <option name='opt2' value='entire' />
            <option name='opt3' value='world' />
          </options>
        </context>
        <defaultTab>nodes</defaultTab>
        <description></description>
        <executionEnabled>true</executionEnabled>
        <id>1442dbb7-55e3-45c0-af4b-a58ce5c07582</id>
        <loglevel>INFO</loglevel>
        <name>ParentJob</name>
        <nodeFilterEditable>false</nodeFilterEditable>
        <plugins />
        <scheduleEnabled>true</scheduleEnabled>
        <sequence keepgoing='false' strategy='node-first'>
          <command>
            <jobref name='PythonChildJob' nodeStep='true'>
              <arg line='-arg1 ${option.opt1} -arg2 ${option.opt2} -arg3 ${option.opt3}' />
              <uuid>159e14d6-29e2-4fe9-b9b3-b1621d59843d</uuid>
            </jobref>
          </command>
        </sequence>
        <uuid>1442dbb7-55e3-45c0-af4b-a58ce5c07582</uuid>
      </job>
    </joblist>
    

    【讨论】:

    • 在实施您建议的更改后,我更新了我的问题。
    • 我用一个例子更新了我的答案 :-) 希望它有所帮助!
    • 感谢您的帮助,同时我也从这个帖子中获得了帮助:groups.google.com/g/rundeck-discuss/c/h6XAdkyNWO8/m/…
    • 那么好消息! :-)
    猜你喜欢
    • 1970-01-01
    • 2015-01-05
    • 2023-03-15
    • 2021-07-28
    • 2015-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-12
    相关资源
    最近更新 更多