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.
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.
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.
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
Thanks for reporting this, we have updated the docs page to reflect what is actually returned in the Octopus.Environment.MachinesInRole[_role_] variable.
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:
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"].
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.