Hey Jeremy,
So hard coding it without quotes worked for me as well. That was literally the only thing I hadn’t tried yet.
I had it in a variable and it wouldn’t work. I tried single and double quotes, even double double quotes. But this works, so that’s sufficient.
The use case is that the custom step template we use would be updated to perform a specific function for each service/site being deployed across multiple servers, but we only want it to happen once regardless of how many servers are shown in the role for the environment.
Write-Host “Machine Id”
Write-Host $OctopusParameters[“Octopus.Machine.Id”]
Write-Host “MachinesInRole”
$Ids = ($OctopusParameters[“Octopus.Environment.MachinesInRole[Web Server]”] -split “,”) | % { $_.Trim() }
If ($Ids[0] -eq $OctopusParameters[“Octopus.Machine.Id”]) {
// code goes here
}
So the above works for what I need as long as it’s ran on a server with that role, which is fine. If I run it on a worker without the role, say I wanted to create a variable with the machine id to run on, it will result in an empty string like every other time I tried. Depending on how this is done in the background, it may just be because I was only firing this one step off and another step with the specified role did not exist at that time.
Just for posterity these are the variations I tried that all came back with an empty array/string:
$OctopusParameters[“Octopus.Environment.MachinesInRole[‘Web Server’]”]
$OctopusParameters[“Octopus.Environment.MachinesInRole[”“Web Server”"]"]
$Role = “Web Server”
$OctopusParameters[“Octopus.Environment.MachinesInRole[$Role]”]
$OctopusParameters[“Octopus.Environment.MachinesInRole[’$Role’]”]
$OctopusParameters[“Octopus.Environment.MachinesInRole[”"$Role""]"]
The following did work when I tried it again when running on a server with the specified role:
$Role = “Web Server”
$OctopusParameters[“Octopus.Environment.MachinesInRole[$Role]”]
I did check the System Variables link you posted beforehand, but this is literally all the documentation on how to use it:
Octopus.Environment.MachinesInRole[_role_]
Lists the machines in a specified role being deployed to
Wasn’t inherently clear in how to specify the role’s name correctly which lead to a few hours of attempts, web searches, and ultimately the posting of this thread.
The description of “Lists the machines in a specified role being deployed to” also doesn’t make immediate sense as if it only works on a role being deployed to / ran on, why do I have to specify it at all? Why doesn’t this work regardless if it’s running on a machine with the identified role or not?
But like I said, the example you provided of not using any quotes got it work in the necessary scenario so my immediate issue is resolved.
Much appreciated,
Jonathan Hunter