1

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?

TobyB
  • 67
  • 1
  • 9
  • `-Body` expects an object but you are sending in a string. Remove `| ConvertTo-Json` and use just `$body = @{}` for an empty body. Use `$body = @{VMName="xyz"; CPU="cpu"; Memory ="Memory"}` for passing in the variables – Jawad Feb 25 '20 at 17:33

3 Answers3

5

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>
MegaDrive68k
  • 3,768
  • 2
  • 9
  • 51
3

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.

TobyB
  • 67
  • 1
  • 9
0

a bit more concise version for PowerShell:

$WebHookURI = 'http://mywebhookuri#myjobname'
$header = @{}
$header.add("Content-Type", "application/json") 
$body = @{
field1 = "$VMName";
field2 = "$CPU";
field3 = "$Memory"
} | ConvertTo-Json
$result = Invoke-RestMethod -Method 'POST' -Uri $WebHookURI -Body $body -Headers $header
sigfried
  • 49
  • 1
  • 6