How to pass options to Rundeck job webhook URL

谁说胖子不能爱 提交于 2021-02-17 02:43:21

问题


I have defined a webhook in Rundeck to run a particular job. This job has 3 options defined: ${option.VMName}, ${option.CPU} and ${option.Memory}. The job itself is defined as a local powershell script and executes as: powershell ${scriptfile} ${option.VMName} ${option.CPU} ${option.Memory}. This is tested and works fine.

I would now like to invoke the webhook POST URL so that the job is remotely triggered (from a web dashboard, using PowerShell) with these options defined. I tried, unsuccessfully, adding the options to the end of my URL:

http://mywebhookuri#myjobname?opt.VMName=$VMName&opt.CPU=$CPU&opt.Memory=$Memory
http://mywebhookuri#myjobname?VMName=$VMName&CPU=$CPU&Memory=$Memory

The following PowerShell code is being used to invoke the 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

The documentation for the webhook plug-in and run-job usage state that "The JSON that is received by the plugin can be used to supply options, node filter, and the Run As user", but doesn't show a clear example of either.

How do I successfully pass these options to the webhook URL?


回答1:


Following the documentation, you need to define the option in this way, and later call passing a JSON data, I did an example but using cURL:

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

That is for this Job Definition example:

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



回答2:


To add some detail to MegaDrive68k's accepted answer (as this is essentially two questions):

I added the following to the "Options" field in the Rundeck webhook definition:

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

And the PowerShell code was modified as follows:

$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

With these changes I was able to successfully invoke the Rundeck webhook with options.



来源:https://stackoverflow.com/questions/60400065/how-to-pass-options-to-rundeck-job-webhook-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!