Use variable in step templates

How would I use a variable in a step template?

I find that if I use the following inside a script that octopus executes, it works as expected. However if I attempt to use the variable inside a step template it fails.

[System.Net.Dns]::GetHostAddresses("$($OctopusParameters['Octopus.Machine.Hostname'])").IpAddressToString[1]

Hi Sowen,

Thanks for reaching out. On the attached screenshot you are putting Powershell code on a “Custom Expression” field which is not quite correct. Those fields can only handle Octopus variables in the format #{VariableName}.

You need to put that PS logic on the step template body itself, and then evaluate if a value was passed to the field “LTM Member IP” or not before running it. Code will say more than a 1000 words here, so I’ve written an example step template for you :slight_smile:

{
  "Id": "ActionTemplates-261",
  "Name": "Get IP of Machine",
  "Description": null,
  "ActionType": "Octopus.Script",
  "Version": 1,
  "Properties": {
    "Octopus.Action.Script.Syntax": "PowerShell",
    "Octopus.Action.Script.ScriptSource": "Inline",
    "Octopus.Action.RunOnServer": "false",
    "Octopus.Action.Script.ScriptBody": "If([string]::ISNullOrWhiteSpace($LTMMemberIP)){\n    \"No IP Adress was provided on the 'LTM Member IP`, using [System.Net.Dns]::GetHostAddresses to resolve it\"\n    [System.Net.Dns]::GetHostAddresses(\"$($OctopusParameters['Octopus.Machine.Hostname'])\").IpAddressToString[1]\n}\nElse{\n    \"Using IPAdress [$LTMMemBerIP]\"\n}",
    "Octopus.Action.Script.ScriptFileName": null,
    "Octopus.Action.Package.FeedId": null,
    "Octopus.Action.Package.PackageId": null
  },
  "Parameters": [
    {
      "Id": "101bee53-ab79-4f61-92bb-62f023f64d38",
      "Name": "LTMMemberIP",
      "Label": "LMT Member IP",
      "HelpText": "IP Address of F5 Pool member. If field is left blank, Octopus will use the static method `GetIPAddress` from `System.Net.DNS` to resolve the machine's IP Address.",
      "DefaultValue": null,
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    }
  ],
  "$Meta": {
    "ExportedAt": "2016-09-16T23:15:04.861Z",
    "OctopusVersion": "3.4.9",
    "Type": "ActionTemplate"
  }
}

Let me know if it doesn’t make sense to you and I’ll explain it further.

Cheers!
Dalmiro

Thanks, I made a few tweaks and this works great:

$Pool = $OctopusParameters['PoolName'];
If ([string]::IsNullOrWhiteSpace($OctopusParameters['MemberIP'])) { 
    Write-Host "No IP Adress was provided on the 'LTM Member IP`, using [System.Net.Dns]::GetHostAddresses to resolve it"
    # On most systems IpAddressToString[0] = ipv6 address, IpAddressToString[1] is first ipv4 address
    $ip = [System.Net.Dns]::GetHostAddresses("$($OctopusParameters['Octopus.Machine.Hostname'])").IpAddressToString[1]
} Else {
    $ip = $OctopusParameters['MemberIP']
}

$Member = $ip+":"+$OctopusParameters['MemberPort'] 
Write-Host "Member is $Member"
```

glad to hear it helped :slight_smile:

Cheers