I would like to run a runbooks via the REST API. It works perfectly when I don’t need to set the variables values and use values in the runbook snapshot. To do so I use this body payload :
I have some prompt variables to set before to call the runbook. How can I set these values ? The way I found is to create a specific snapshot with the values excpected. But I would prefer to specify theses values in the payload above.
This functionality exists, it’s just not as easy as giving it the variable name, its an element name which is a not-so-pretty looking identifier. For example, for one of my prompted variables, the identifier is “5153dd58-d132-a8b1-aa57-2dc7befcd020”. You would need to implement code to search on the elements of the runbook to find the desired identifiers, then you could create an object with the prompted variables to add to the POST call.
Here is some sample code for 1 prompted variable, hardcoded. You could add a lot of logic for multiple variables and other things if necessary.
$OctopusServerUrl = ""
$ApiKey = ""
$spaceId = "Spaces-1"
$runbookId = "Runbooks-141"
$runbookSnapshot = "RunbookSnapshots-162"
$environmentId = "Environments-1"
$variablename = "promptedvar"
$newValue = "Any value you want it to be"
#goes and grabs the values of the runbook elements
$elements = Invoke-WebRequest "$($OctopusServerUrl)/api/$($spaceid)/runbooks/$($runbookid)/runbookRuns/preview/$($environmentid)?includeDisabledSteps=true" -Headers @{"X-Octopus-ApiKey"="$ApiKey"}
$elements = $elements | convertfrom-Json
#This finds the element ID you need to put into the jsonbody for the runbook
$element = $elements.Form.Elements | Where-Object { $_.Control.Name -eq $variablename }
write-host $element.Name
#Create jsonbody to run the runbook with the values.
$jsonbody = @{
RunBookId = $runbookId
RunbookSnapshotId = $runbookSnapshot
EnvironmentId = $environmentId
FormValues = @{
"$($element.Name)" = "$($newValue)"
}
} | ConvertTo-Json
#run the runbook
Invoke-RestMethod -Method "POST" "$($OctopusServerUrl)/api/$($spaceid)/runbookRuns" -body $jsonbody -Headers @{"X-Octopus-ApiKey"="$ApiKey"}
As always, please test code support gives you or you get off our example repository thoroughly before using in a production environment as it’s not guaranteed to work for all scenarios.
Please let me know if that works for you or if you need further assistance with it.
$OctopusServerUrl = ""
$ApiKey = ""
$spaceId = "Spaces-1"
$runbookId = "Runbooks-141"
$runbookSnapshot = "RunbookSnapshots-181"
$environmentId = "Environments-1"
$variablename = "promptedvarlib"
$newValue = "library variable set works"
#goes and grabs the values of the runbook elements
$elements = Invoke-WebRequest "$($OctopusServerUrl)/api/$($spaceid)/runbooks/$($runbookid)/runbookRuns/preview/$($environmentid)?includeDisabledSteps=true" -Headers @{"X-Octopus-ApiKey"="$ApiKey"}
$elements = $elements | convertfrom-Json
#This finds the element ID you need to put into the jsonbody for the runbook
$element = $elements.Form.Elements | Where-Object { $_.Control.Name -eq $variablename }
#Create jsonbody to run the runbook with the values.
$jsonbody = @{
RunBookId = $runbookId
RunbookSnapshotId = $runbookSnapshot
EnvironmentId = $environmentId
FormValues = @{
"$($element.Name)" = "$($newValue)"
}
} | ConvertTo-Json
#run the runbook
Invoke-RestMethod -Method "POST" "$($OctopusServerUrl)/api/$($spaceid)/runbookRuns" -body $jsonbody -Headers @{"X-Octopus-ApiKey"="$ApiKey"}
I’m not clear where the wires are crossed. One API doesn’t show any vars, the other says they are required even if I pass them in. You can see “SSLWildCard” and “Teams” are prompted/required vars from the runbook.
It looks like it was a bug with runbooks where the library prompted variable wouldn’t show up until you publish the runbook.
I published the runbook and then got results within the portal. However, the code still did not work on my end for library set prompted variables, so I believe the GH Issue resolution also mended the broken connection in the API calls.
Would you be able to upgrade to latest and give it a shot?
Success! That was definitely the root of our issue. I’m now seeing the the variables, and can trigger this runbook through the API. This gets me unblocked.
Thank you again, Jeremy. Really appreciate your time and quick responses.