Goal
Replace a library variable set that contains duplicate variable IDs with a new set whose variables each have a unique ID, so you can change and save its variables again.
When two variables in a library variable set share an ID, Octopus can't save any change to the set. The Octopus Web Portal and the REST API both return this error:
An item with the same key has already been added. Key: <variable ID>
A bug in older versions of Octopus Server could give two variables in a library variable set the same ID. Upgrading Octopus Server doesn't repair a set that's already affected, so the error can appear long after the bug itself was fixed.
The existing set can't be repaired in place, because every save fails. Instead, you find the duplicates, copy the variables into a new set with a new name, and move your projects to the new set. These steps only read the existing set. They never change it.
Prerequisites
You'll need:
- An Octopus API key for a user who can view, create, and edit library variable sets, and edit the projects that use the set. Learn more about user roles and permissions.
- PowerShell 7 or later on the machine you run the commands from.
- The name of the library variable set to repair, and the name of the space it belongs to (for example,
Default). - A name for the new library variable set that no other library variable set in the space uses.
- The current values of any sensitive variables in the set. Octopus never returns sensitive values through the API, so you'll enter them again in the new set.
- The current tenant values for any variable templates in the set. Tenant values belong to the existing set, so you'll enter them again for the new set.
Steps
Projects keep using the existing library variable set until you move them to the new one. Carry out these steps at a time when no deployments or runbook runs depend on changes to the set.
To repair duplicate variable IDs in a library variable set:
- On the machine you run PowerShell from, save this script as
copy-library-variable-set.ps1:
# Copies a library variable set with duplicate variable IDs into a new library variable set.
# Only reads the existing set. Requires PowerShell 7 or later.
# --- Settings: change these ---
$octopusUrl = "https://your-octopus-url"
$apiKey = "API-YOURAPIKEY"
$spaceName = "Default"
$libraryVariableSetName = "Your library variable set" # existing set, read only
$newLibraryVariableSetName = "Your library variable set (fixed)" # new set to create
$dryRun = $true # $true = report only, $false = create the new set
$ErrorActionPreference = "Stop"
$headers = @{ "X-Octopus-ApiKey" = $apiKey }
# Find the space
$space = (Invoke-RestMethod -Method Get -Uri "$octopusUrl/api/spaces/all" -Headers $headers) | Where-Object { $_.Name -eq $spaceName }
if (-not $space) { throw "Space '$spaceName' not found." }
$spaceId = $space.Id
function Invoke-Octopus($Method, $Path, $Body) {
$params = @{ Method = $Method; Uri = "$octopusUrl/api/$spaceId/$Path"; Headers = $headers }
if ($Body) { $params.Body = ($Body | ConvertTo-Json -Depth 10); $params.ContentType = "application/json" }
try { Invoke-RestMethod @params }
catch { throw "$Method $Path failed: $($_.ErrorDetails.Message ?? $_.Exception.Message)" }
}
# find the set
$oldSet = (Invoke-Octopus Get "libraryvariablesets?partialName=$([uri]::EscapeDataString($libraryVariableSetName))&take=100").Items |
Where-Object { $_.Name -eq $libraryVariableSetName }
if (-not $oldSet) { throw "Library variable set '$libraryVariableSetName' not found in space '$spaceName'." }
Write-Host "Found $($oldSet.Id) ($($oldSet.VariableSetId))"
# download and back up
$oldVariables = Invoke-Octopus Get "variables/$($oldSet.VariableSetId)"
$backupFile = "variableset-backup-$($oldSet.Id)-$(Get-Date -Format yyyyMMdd-HHmmss).json"
[pscustomobject]@{ LibraryVariableSet = $oldSet; Variables = $oldVariables } | ConvertTo-Json -Depth 10 | Out-File $backupFile
Write-Host "Backed up set and $($oldVariables.Variables.Count) variables to $backupFile"
# list duplicates
$duplicates = $oldVariables.Variables | Group-Object -Property Id | Where-Object { $_.Count -gt 1 }
if (-not $duplicates) { Write-Host "No duplicate variable IDs found. Nothing to do."; return }
Write-Host "`nDuplicate IDs:"
$duplicates | Select-Object @{ Name = "Id"; Expression = { $_.Name } }, Count,
@{ Name = "Variables"; Expression = { ($_.Group.Name | Sort-Object -Unique) -join ", " } } | Format-Table -AutoSize
$sensitiveNames = @($oldVariables.Variables | Where-Object IsSensitive | ForEach-Object Name | Sort-Object -Unique)
Write-Host "Templates to copy: $(@($oldSet.Templates).Count) Sensitive variables to re-enter: $($sensitiveNames.Count)"
if ((Invoke-Octopus Get "libraryvariablesets?partialName=$([uri]::EscapeDataString($newLibraryVariableSetName))&take=100").Items |
Where-Object { $_.Name -eq $newLibraryVariableSetName }) { throw "A library variable set named '$newLibraryVariableSetName' already exists." }
if ($dryRun) { Write-Host "`nDry run: no changes made. Set `$dryRun = `$false to apply."; return }
# create the new set with the original description and templates
$templates = @($oldSet.Templates | ForEach-Object { $_.Id = $null; $_ })
$newSet = Invoke-Octopus Post "libraryvariablesets" @{
Name = $newLibraryVariableSetName; Description = $oldSet.Description; ContentType = "Variables"; Templates = $templates
}
Write-Host "Created $($newSet.Id) ($($newSet.VariableSetId)) named '$($newSet.Name)'"
# copy every variable with a new ID
$newVariables = Invoke-Octopus Get "variables/$($newSet.VariableSetId)"
$newVariables.Variables = @($oldVariables.Variables | ForEach-Object { $_.Id = [guid]::NewGuid().ToString(); $_ })
Invoke-Octopus Put "variables/$($newSet.VariableSetId)" $newVariables | Out-Null
Write-Host "Copied $($newVariables.Variables.Count) variables into $($newSet.Id)"
# Verify
$check = Invoke-Octopus Get "variables/$($newSet.VariableSetId)"
Write-Host "`nVariables in backup: $($oldVariables.Variables.Count) in new set: $($check.Variables.Count)"
if ($check.Variables | Group-Object -Property Id | Where-Object { $_.Count -gt 1 }) { Write-Warning "Duplicate IDs remain in the new set." }
else { Write-Host "Every variable in the new set has a unique ID." }
Write-Host "`nStill to do in the portal:"
if ($sensitiveNames) { Write-Host "- Re-enter sensitive values in the new set: $($sensitiveNames -join ', ')" }
if (@($oldSet.Templates).Count) { Write-Host "- Re-enter tenant values for the new set's variable templates" }
Write-Host "- Move projects listed on '$($oldSet.Name)' > Usage to '$($newSet.Name)'"
-
In
copy-library-variable-set.ps1, change the values in theSettingsblock to your Octopus Server URL, API key, space name, the name of the existing library variable set, and the name for the new one. -
In a terminal, run the script with
$dryRunset to$true:
pwsh ./copy-library-variable-set.ps1
The script saves a backup of the existing set to a JSON file and lists the duplicated variable IDs, the variables that share each one, and how many templates and sensitive variables the new set will need. The ID in the error message appears in this list.
-
In
copy-library-variable-set.ps1, set$dryRunto$false. -
In the same terminal, run the script again:
pwsh ./copy-library-variable-set.ps1
The script creates the new library variable set with the existing set's description and variable templates, and copies every variable into it with a new ID. Both variables that shared an ID are copied.
-
In the Octopus Web Portal, go to Deploy > Variable Sets, open the new library variable set, and enter the value of each sensitive variable again.
-
In each tenant connected to a project that uses the set, enter the tenant values for the new set's variable templates.
-
In each project that uses the existing set, include the new library variable set and remove the existing one. The existing set's Usages tab lists these projects.
Verify
When the script finishes, its output shows the same number of variables in the backup and in the new set, followed by Every variable in the new set has a unique ID.
In the Octopus Web Portal, open the new library variable set, change any variable, and select Save. The set saves without the duplicate key error, and its Usages tab lists every project that used the existing set.
Related
- Library variable sets explains how library variable sets share variables across projects.
- Sensitive variables explains how Octopus stores and protects secret values.
- Variables API examples has more scripts for reading and changing variables with the REST API.
- Octopus REST API covers authentication and how the API is structured.