MachinesInRole

Hi, I’m trying out OctopusDeploy for the first time, so please bear with me if I’m doing something daft.

I want to deploy to one or more machines with various roles, say RoleA, RoleB, RoleC.
I have a test environment with one machine that has all the roles, and a production environment with 3 machines with one role each.
When I install I want to configure the software with the location of the various components.

The documentation on variables tells me there is a variable Octopus.Environment.MachinesInRole[role]
This looks like it should do the job for me, but when I use it, it evaluates to ‘machines-1’, which is the ID one of my defined machines.
Given the machine IDs, how do I get the hostnames?

Tom.
PS
I think I can do this using scoped variables that name the machine with each role - e.g.
RoleAServer = testMachine, RoleBServer = testMachine, RoleCServer = testMachine (in the test environment scope)
RoleAServer = Production1, RoleBServer = Production2, RoleCServer = Production3 (in the test production scope)
But if I do this I’ll have to keep the variables lined up with the environment configuration manually.

Hi Tom,

Thanks for getting in touch! You are correct we do not have any variable available for hostname, and no way to get it even with the machine ID.
Could you explain a bit about what you are trying to accomplish in a bit more detail and we might be able to come up with a workaround.
There is nothing to stop you from naming the machine as the hostname, and Octopus.Machine.Name is a variable, but if you need a list of all machine hostnames in a single location this may not be available to you.
We have another fairly convoluted option that could be done with powershell but first maybe understanding the problem in more detail before I head down that road.

Vanessa

Presumably if I use Octopus.Machine.Name from the Deployment Script I’ll get the name of the machine that the Deployment Script is running on. That isn’t what I need. I need the name of the machine with a particular role so I can configure the software I’m installing on the local machine to point to it. For example to configure my webserver to point to my DBserver. In my test environment they might both be on the same machine, and in my production environment they might be on different machines.

I believe I can achieve this using, say, a DBserver variable defined for each environment, but that means any time I modify my environment I have to define a new variable scoped to the new environment. But the information I need is already in the system. Having to define it in two different ways leaves room for getting it wrong down the line. (I may also have lots of test environments with different characteristics, therefore lots of opportunity for the variable settings and environment settings to get out of step.)

The Octopus.Environment.MachinesInRole variable comes close to what I need, but returns an ID that I don’t seem to be able to do anything with. If it returned the machine name, or if there were an Octopus.Machine.id.property variable, I think I’d have what I need.

Hi Tom,

It’s a bit of a hack, but you could do something like this:

  1. Create a PowerShell step that runs on your DBServer. It sets an output variable to the value of Octopus.Machine.Name
  2. In your package step, use that output variable instead of Octopus.Machine.Name

The following link demonstrates how output variables from one step can be used in a subsequent step:

Paul

It looks like the documentation for the Octopus.Environment.MachinesInRole[role] variable is incorrect: http://docs.octopusdeploy.com/display/OD/System+variables

It seems to indicate that the variable should be returning the Octopus.Machine.Name value, but instead it’s returning the Octopus.Machine.Id value.

Octopus.Environment.MachinesInRole[_role_]  WEBSRV01,WEBSRV02	Lists the machines in a specified role
Octopus.Machine.Id   machines-123	The ID of the machine
Octopus.Machine.Name  WEBSVR01	The name of the machine

-Joe

Hi Joe,

Thanks for reporting this, we have updated the docs page to reflect what is actually returned in the Octopus.Environment.MachinesInRole[_role_] variable.

Henrik

Hi Henrik,

Can the machine ID be used for anything useful?
Wouldn’t changing the code to match the original documentation be more useful?

Tom

Hi Tom,

We use these machine ID’s to determine what machines will be included in deployments etc.

Henrik

Ran into a similar issue. The documentation is sill incorrect regarding the machine in role:

Is it possible somehow to get the machines in a given Role and get the “Name” (host name) from “ID”

Hi Alan,

Thanks for getting in touch!

I’ve had a look at the documentation page and can’t see where it is incorrect still, could you please point me to what is incorrect?

In regards to getting the machine names from machine IDs isn’t all that easy unfortunately.

You could:

  1. Create a PowerShell step at the beginning of your deployment process that runs on the role that you want to get the host names from with the following code Set-OctopusVariable -name "<MyMachineHostNameVariable>" -value $OctopusParameters["Octopus.Machine.Name"].
  2. Then in a later step:
$MatchRegex = "Octopus\.Action\[<stepname>\]\.Output\[(.*?)\]\.<MyMachineHostNameVariable>"

Write-Host "Machine names:"
$OctopusParameters.GetEnumerator() | Where-Object { $_.Key -match $MatchRegex } | % { 
  Write-Host $_.Value
  # Do what you need to do with your host names that are in the specified role
}

It’s not pretty, but should hopefully give you an idea of how you can achieve your goal.

Hope that helps!

Henrik