微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Rundeck 将通用参数/变量传递给流 使用 python3 示例更新

如何解决Rundeck 将通用参数/变量传递给流 使用 python3 示例更新

考虑到我有一个主要的流程执行工作,我将来会有更多这样的工作。

我将有几个后端流程作业,它们将用作我上面提到的主要流程执行作业中的参考作业。

我需要完成的事情:

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

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

#Update:

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

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

enter image description here

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

也喜欢这样${option.source_details}

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

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

解决方法

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

我举个例子:

<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>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。