Replace a Powershell variable inside Octopus Variable

server
usability
(Saurabh Ghildiyal) #1

I have to create a list of output variables which will contain list of my targets as per every role. First issue is #{Octopus.Machine.Roles} variable does not work from octopus server. Is there any way to take out list of roles with script running from octopus server and not targets?
The other issue is, even if I hard code the role in a Powershell variable I am not sure how to pass it to octopus variable, tried few things like this but always gives me a blank value:

$role = “AppServers”
$machineIds = $OctopusParameters[‘Octopus.Environment.MachinesInRole[_$role_]’]
write-host $machineIds

Please suggest.

(Kenneth Bates) #3

Hi Saurabh,

Thanks for getting in touch! I think this unexpected behavior you’re hitting would come down to the fact that these variables aren’t populated when run on the server, as they pull the values based on the context of the deployment. If you instead configure the step to run on the server on behalf of roles, that should give you the result you’re after.

Alternatively, you could also possibly create some output variables on a prior step that does run on the role, and save the values. You could then call that in the script step that runs on the server.

Does that help get you going as needed? Let me know what you think or if you have any further questions going forward. :slight_smile:

Best regards,

Kenny

(Saurabh Ghildiyal) #4

Thanks for the reply Kenneth, anything I can do about the second part of my query?
Let’s say I have got the list of roles and now I want to get the machine names in it:

$roles = “AppServers,DBServers”
foreach($role in $roles.Split(’,’))
{
$machineIds = $OctopusParameters[‘Octopus.Environment.MachinesInRole[$role]’].Split(’,’)
$tempArray = @()
foreach ($machineId in $machineIds) {
$machine = $repository.Machines.Get($machineId)
$tempArray += $machine.Name
}
}

I am not sure how to pass $role to an octopus variable.

(Kenneth Bates) #6

Hi @saurabh.ghildiyal,

Thanks for keeping in touch! Admittedly I’m not a PowerShell expert, but nothing in your pasted script glares out as an issue. You can certainly pass in a PowerShell variable in this way, and I’ve confirmed that with the very simple following script:

$role = "cloud"
$MachinesInRole = $OctopusParameters["Octopus.Environment.MachinesInRole[$role]"]
write-host "Machines in role: $MachinesInRole"

This printed correctly: Machines in role: Machines-1, Machines-2.

I hope that helps narrow down the issue! If you have any questions or concerns moving forward, please don’t hesitate to reach out.

Best regards,

Kenny

(Saurabh Ghildiyal) #7

Thanks for the help Kenneth, the double quotes that you have used did the trick.

(Kenneth Bates) #8

Hi Saurabh,

Thanks for keeping in touch and letting me know! Great to hear that helped, and please don’t hesitate to reach out if you have any questions or concerns in the future!

Best regards,

Kenny

(system) closed #9

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.