0

I want to pass a value in Octopus from one step to another of a project via output variable, the value is "VM running" or "VM deallocated". There are two server, one of the server is down, another one is running so values should be passed accordingly. Now when I use the exact syntax of Output variable, it is passing Null value to next step.

Octopus deploy Project Step 1:

$RG = $RGName
$VM = "#{StepTemplate_VMName}"
$WarningPreference = 'SilentlyContinue'
$VMStats = (Get-AzureRmVM -Name $VM -ResourceGroupName $RG -Status).Statuses
$stats = ($VMStats | Where Code -Like 'PowerState/*')[0].DisplayStatus

Set-OctopusVariable -name "RunStatus" -value $stats
write-host $stats     #value can either be "VM running" or "VM deallocated"

Octopus deploy Project Step 2:

$VM = "#{StepTemplate_VMName}"
$Runstatus = $OctopusParameters["Octopus.Action[Step1].Output[$VM].RunStatus"]
write-host $Runstatus

If I do not use [$VM] in the code of step 2, it give only 1 value to both the machine as "VM running" As per the syntax given in Octopus website, we should use the VM name to pass machine specific different values. so I used [$VM] but it gives null values to both of the machine

Edit: Event If I hardcode the value of $VM to any one VMName, it still gives me null.

Jessica
  • 167
  • 2
  • Is the `write-host` command coming from the documentation? There is a difference in powershell between `Write-Host` (write to the console) and `Write-Output` (pass to the pipeline). Can you try using `Write-Output`? (More info [here](https://stackoverflow.com/a/19754384/3797799)) – colinD Apr 28 '20 at 07:16
  • its still giving me null value, I think I am missing something at substitution of [$VM]. Something called as Variable binding syntax may be incorrect there. – Prathamesh Wagh Apr 28 '20 at 08:17
  • Can you confirm that $VM is the name of you deployment target? – Michéle Johl Apr 28 '20 at 08:40
  • yes, I have more than 1 deployment target so used a variable, I checked Raw log and found that the $VM is getting replaced by VM name, but still variable $Runstatus is giving null value – Prathamesh Wagh Apr 28 '20 at 09:01
  • Is the VM Name exactly the same as your deployment target name? Based on the documentation the output array will use the deployment target name. So if you want to use Output[$VM], then $VM must match the deployment target name 100%. – Michéle Johl Apr 28 '20 at 09:12
  • yes, its the exact match, I also tried using VM names instead of $VM, it still gives me null value – Prathamesh Wagh Apr 28 '20 at 09:21

2 Answers2

0

Based on what I do in Octopus it should be:

$Runstatus = $OctopusParameters["Octopus.Action[Step1].Output.RunStatus"]

Even states that format on the help documentation. https://octopus.com/docs/projects/variables/output-variables

  • I agree, that will pass on only 1 value for both the machines. I want to pass two different values which should be machine specific. For this requirement octopus have suggested to add machine name into that command like this - Octopus.Action[StepA].Output[Web01].TestResult – Prathamesh Wagh Apr 28 '20 at 08:07
  • If your scope is set correctly you should get the value for the deployment target correctly. – Michéle Johl Apr 28 '20 at 08:23
0

Is the step that's creating the output variable actually being run on the server that's down? When the output variable is created in a step that's run on multiple machines, multiple machine-scoped values for that variable will be created. When the subsequent step is run on the same machines, Octopus will pick the correct value based on that scope. So maybe a little off topic, but if both steps are run on both targets, specifying the $VM scope is redundant, and is only needed if you want a different machine's value.

Since hardcoding the target name here returns null it seems like the output variable creation step wasn't run on that target. I'd suggest debugging this by creating a variable called OctopusPrintEvaluatedVariables with value True in your project. That'll increase the task log verbosity and log how all variables are evaluated through each step of the deployment.

SLTemplezz
  • 41
  • 1
  • I am running the script on Octopus server not on deployment target. I have configured the step 1 and step 2 to run on octopus server. I did enabled the logging by adding the variable but still no luck – Prathamesh Wagh Apr 29 '20 at 08:11