Skip to content

Instantly share code, notes, and snippets.

@geekzter
Last active May 24, 2021 08:35
Show Gist options
  • Save geekzter/dc942004190cad3ddc5a53ce52c07d0b to your computer and use it in GitHub Desktop.
Save geekzter/dc942004190cad3ddc5a53ce52c07d0b to your computer and use it in GitHub Desktop.
Azure VM scale set environment variables for Terraform input
- pwsh: |
# 1. Use pipeline agent VNet as network to peer from
$env:TF_VAR_peer_network_id ??= $env:GEEKZTER_AGENT_VIRTUAL_NETWORK_ID
# 2. Set random CIDR (to reduce the risk of clashing VNet peerings with agent VNet)
$env:TF_VAR_address_space ??= "$([IPAddress]::Parse(`
[String] (`
167772160 + (`
65536*(`
Get-Random -Minimum 0 -Maximum 255 -SetSeed $(Build.BuildId)`
)`
)`
)`
) | Select-Object -ExpandProperty IPAddressToString)/16"
# Set unique resource suffix
$env:TF_VAR_resource_suffix ??= "$(Build.BuildId)"
# Convert uppercased Terraform environment variables to Terraform arguments
foreach ($tfvar in $(Get-ChildItem -Path Env: -Recurse -Include TF_VAR_*)) {
$terraformVariableName = $tfvar.Name.Substring(7).ToLowerInvariant()
$terraformVariableValue = $tfVar.Value
$varArgs += " -var ${terraformVariableName}=${terraformVariableValue}"
}
Write-Host "##vso[task.setvariable variable=variableOption;isOutput=true]$varArgs"
name: terraformVariables
displayName: 'Prepare Terraform variables'
workingDirectory: '$(terraformDirectory)'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment