Run a runbook with prompt variables

Hello,

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 :

{
    "RunbookId":"Runbooks-1",
    "RunbookSnapshotId":"RunbookSnapshots-47",
    "ProjectId":"Projects-21",
    "EnvironmentId":"Environments-2"
}

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.

In my dreams, I would expect something like that :slight_smile:

{
    "RunbookId":"Runbooks-1",
    "RunbookSnapshotId":"RunbookSnapshots-47",
    "ProjectId":"Projects-21",
    "EnvironmentId":"Environments-2",
    "Variables" : {
           "Variable1":"Value1",
           "Variable2":"Value2"
    {
}

Thanks for your help :wink: !!

Hi Najim,

Welcome to the Octopus Forums!

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.

Thanks,
Jeremy

Hi,

Thank you for your help, it works perfectly. I’m now able to specify prompted variables values when I call the API ;).

Have a nice day.

1 Like

Hi,

You’re very welcome, thank you for letting me know it resolved the issue for you.

I hope you have a great day as well.

Best,
Jeremy

I tried the script above, and don’t get the same results. My form node is blank and my runbook has prompted variables:

“Form”: {
“Values”: {},
“Elements”: []
}

Should prompted variables in a link Library Variable Set show up when calling this preview API?

Hey @jturcic,

Welcome to the Octopus Forums.

I did a quick test by adding a prompted library variable within a set and was able to achieve the same results:



Code:

$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"}

Output:

I’m on 2020.3.3. Is yours setup like mine?

If so, which version are you on?

Thanks,
Jeremy

Thanks for the welcome.

We are on 2020.1.15. My setup does indeed look like yours, and in the variable set in the UI you can see all of the vars from the linked set:

And as you can see, when I post to the runbook it says these vars are required (even though I am including them):

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.

You’re very welcome!

What is the output if you run this code in powershell? (just input your server URL and API Key)

$OctopusServerUrl = ""
$ApiKey = ""
$spaceId = "Spaces-1"
$runbookId = "Runbooks-21"
$runbookSnapshot = "RunbookSnapshots-45"
$environmentId = "Environments-563"
$variablename = "SSLWildCard"

$elements = Invoke-WebRequest "$($OctopusServerUrl)/api/$($spaceid)/runbooks/$($runbookid)/runbookRuns/preview/$($environmentid)?includeDisabledSteps=true" -Headers @{"X-Octopus-ApiKey"="$ApiKey"}
$elements = $elements | convertfrom-Json

$element = $elements.Form.Elements | Where-Object { $_.Control.Name -eq $variablename }
write-host $element

$elements is empty

PS /Users/jturcic> $spaceId = "Spaces-1"
PS /Users/jturcic> $runbookId = "Runbooks-21"
PS /Users/jturcic> $runbookSnapshot = "RunbookSnapshots-45"
PS /Users/jturcic> $environmentId = "Environments-563"
PS /Users/jturcic> $variablename = "SSLWildCard"
PS /Users/jturcic> 
PS /Users/jturcic> $elements = Invoke-WebRequest "$($OctopusServerUrl)/api/$($spaceid)/runbooks/$($runbookid)/runbookRuns/preview/$($environmentid)?includeDisabledSteps=true" -Headers @{"X-Octopus-ApiKey"="$ApiKey"}
PS /Users/jturcic> $elements = $elements | convertfrom-Json                                                                                                                                                                                            
PS /Users/jturcic> $element = $elements.Form.Elements | Where-Object { $_.Control.Name -eq $variablename }
PS /Users/jturcic> write-host $element                                                                                               

PS /Users/jturcic>

Hi,

I think I’ve found the issue. I spun up your version to repro.

The library variable set prompted variable isn’t even being considered when running it manually via the portal.


I searched our GitHub issues and this is the related issue: https://github.com/OctopusDeploy/Issues/issues/6404

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?

Please let me know.

Thanks,
Jeremy

Yes, of course. I will update our server today, test again and report back. Thanks Jeremy!

You’re very welcome! I look forward to hearing the results.

Best,
Jeremy

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. :handshake:

1 Like

You’re welcome! Thanks for letting me know it worked.

I hope you have a great rest of your week.

Best,
Jeremy