如何将选项传递给 Rundeck 作业 webhook URL

How to pass options to Rundeck job webhook URL

我已经在 Rundeck 中为 运行 特定作业定义了一个 webhook。该作业定义了 3 个选项:${option.VMName}、${option.CPU} 和 ${option.Memory}。作业本身被定义为本地 powershell 脚本并执行为:powershell ${scriptfile} ${option.VMName} ${option.CPU} ${option.Memory}。这已经过测试并且工作正常。

我现在想调用 webhook POST URL 以便在定义这些选项的情况下远程触发作业(从 Web 仪表板,使用 PowerShell)。我尝试将选项添加到 URL:

的末尾,但未成功
http://mywebhookuri#myjobname?opt.VMName=$VMName&opt.CPU=$CPU&opt.Memory=$Memory
http://mywebhookuri#myjobname?VMName=$VMName&CPU=$CPU&Memory=$Memory

正在使用以下 PowerShell 代码来调用 webhook:

$WebHookURI = "http://mywebhookuri#myjobname"
$header = @{}
$header.add("Content-Type","text/plain")
$body = @{} | ConvertTo-Json
$result = Invoke-RestMethod -Method Post -Uri $WebHookURI -Body $body -Headers $header

webhook 插件和 运行-job 用法的文档指出 "The JSON that is received by the plugin can be used to supply options, node filter, and the Run As user",但没有显示任何一个的明确示例。

如何将这些选项成功传递给 webhook URL?

按照 documentation, you need to define the option in this 方式,稍后调用传递 JSON 数据,我做了一个示例,但使用 cURL:

curl -H "Content-Type: application/json" -X POST -d '{"field1":"hello world"}' http://yourhost:4440/api/34/webhook/3moY0Ru1zxl5gM0tpVlecJ5BN1LPyhsx#New_Hook

这是针对此职位定义示例的:

<joblist>
  <job>
    <context>
      <options preserveOrder='true'>
        <option name='opt1' />
      </options>
    </context>
    <defaultTab>nodes</defaultTab>
    <description></description>
    <executionEnabled>true</executionEnabled>
    <id>e97efb53-99a6-4e5a-80b7-a1b055866f43</id>
    <loglevel>INFO</loglevel>
    <name>HelloWorld</name>
    <nodeFilterEditable>false</nodeFilterEditable>
    <scheduleEnabled>true</scheduleEnabled>
    <sequence keepgoing='false' strategy='node-first'>
      <command>
        <exec>echo ${option.opt1}</exec>
      </command>
    </sequence>
    <uuid>e97efb53-99a6-4e5a-80b7-a1b055866f43</uuid>
  </job>
</joblist>

为 MegaDrive68k 接受的答案添加一些细节(因为这本质上是两个问题):

我在 Rundeck webhook 定义的 "Options" 字段中添加了以下内容:

-VMName ${data.field1} -CPU ${data.field2} -Memory ${data.field3}

并将PowerShell代码修改如下:

$WebHookURI = 'http://mywebhookuri#myjobname'
$header = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$header.add("Content-Type", "application/json")
$body = "{`n `"field1`" : `"$VMName`",`n `"field2`" : `"$CPU`",`n `"field3`" : `"$Memory`"`n}" 
$result = Invoke-RestMethod -Method 'POST' -Uri $WebHookURI -Body $body -Headers $header

通过这些更改,我能够使用选项成功调用 Rundeck webhook。