-
Notifications
You must be signed in to change notification settings - Fork 0
AzDoVariableGroup
The AzDoVariableGroup DSC resource is used to create and manage variable groups in Azure DevOps. Variable groups allow you to centralize and reuse variables across pipelines and environments, making configuration management easier and more maintainable.
AzDoVariableGroup [string] #ResourceName
{
VariableGroupName = [String] $VariableGroupName
ProjectName = [String] $ProjectName
[ Ensure = [String] {'Present', 'Absent'} ]
[ Variables = [Hashtable] $Variables ]
[ KeyVaultName = [String] $KeyVaultName ]
[ ServiceConnectionName = [String] $ServiceConnectionName ]
[ DependsOn = [String[]] ]
[ PsDscRunAsCredential = [PSCredential] ]
}-
VariableGroupName [String] - The name of the variable group. Must be unique within the project.
-
ProjectName [String] - The name of the project that will contain this variable group.
-
Ensure [String] - Desired state of the resource:
-
'Present'- (default) Variable group should exist -
'Absent'- Variable group should be removed
-
-
Variables [Hashtable] - A hashtable containing the variables to store in the group. Example:
@{ 'Var1' = 'Value1'; 'Var2' = 'Value2' } -
KeyVaultName [String] - Optional. Name of Azure Key Vault to link for secret variables.
-
ServiceConnectionName [String] - Optional. Name of the service connection to use for Key Vault integration.
-
DependsOn [String[]] - Dependencies on other resources. Typically depends on the project existing first.
-
PsDscRunAsCredential [PSCredential] - Credentials to run this resource under.
The resource returns the following properties:
- VariableGroupName - The name of the variable group
- ProjectName - The project containing the variable group
- Ensure - Current state ('Present' or 'Absent')
- VariableGroupId - The unique identifier of the variable group
- Variables - The variables contained in the group
- KeyVaultName - Associated Key Vault (if linked)
- ServiceConnectionName - Associated service connection (if linked)
Configuration CreateBasicVariableGroup {
Import-DscResource -ModuleName 'AzureDevOpsDscNative'
Node localhost {
# First create the project
AzDoProject 'MyProject' {
Ensure = 'Present'
ProjectName = 'MyProject'
SourceControlType = 'Git'
ProcessTemplate = 'Agile'
Visibility = 'Private'
}
# Create variable group
AzDoVariableGroup 'AppSettings' {
Ensure = 'Present'
VariableGroupName = 'App Settings'
ProjectName = 'MyProject'
Variables = @{
'Environment' = 'Production'
'LogLevel' = 'Information'
'MaxConnections' = '100'
'Timeout' = '30'
}
DependsOn = '[AzDoProject]MyProject'
}
}
}
CreateBasicVariableGroup
Start-DscConfiguration -Path ./CreateBasicVariableGroup -Wait -VerboseConfiguration EnvironmentVariableGroups {
Import-DscResource -ModuleName 'AzureDevOpsDscNative'
Node localhost {
AzDoProject 'PipelineProject' {
Ensure = 'Present'
ProjectName = 'PipelineProject'
SourceControlType = 'Git'
ProcessTemplate = 'Agile'
Visibility = 'Private'
}
# Development variables
AzDoVariableGroup 'DevVariables' {
Ensure = 'Present'
VariableGroupName = 'Dev Variables'
ProjectName = 'PipelineProject'
Variables = @{
'Environment' = 'Development'
'ApiEndpoint' = 'https://dev-api.company.com'
'DatabaseServer' = 'dev-db.company.com'
'LogLevel' = 'Debug'
}
DependsOn = '[AzDoProject]PipelineProject'
}
# Staging variables
AzDoVariableGroup 'StagingVariables' {
Ensure = 'Present'
VariableGroupName = 'Staging Variables'
ProjectName = 'PipelineProject'
Variables = @{
'Environment' = 'Staging'
'ApiEndpoint' = 'https://staging-api.company.com'
'DatabaseServer' = 'staging-db.company.com'
'LogLevel' = 'Information'
}
DependsOn = '[AzDoProject]PipelineProject'
}
# Production variables
AzDoVariableGroup 'ProdVariables' {
Ensure = 'Present'
VariableGroupName = 'Prod Variables'
ProjectName = 'PipelineProject'
Variables = @{
'Environment' = 'Production'
'ApiEndpoint' = 'https://api.company.com'
'DatabaseServer' = 'prod-db.company.com'
'LogLevel' = 'Warning'
}
DependsOn = '[AzDoProject]PipelineProject'
}
}
}
EnvironmentVariableGroups
Start-DscConfiguration -Path ./EnvironmentVariableGroups -Wait -VerboseConfiguration VariableGroupWithKeyVault {
Import-DscResource -ModuleName 'AzureDevOpsDscNative'
Node localhost {
AzDoProject 'SecureProject' {
Ensure = 'Present'
ProjectName = 'SecureProject'
SourceControlType = 'Git'
ProcessTemplate = 'Agile'
Visibility = 'Private'
}
# Create service connection for Key Vault
AzDoServiceConnection 'KeyVaultConnection' {
Ensure = 'Present'
ServiceConnectionName = 'Azure KeyVault'
ProjectName = 'SecureProject'
ServiceConnectionType = 'AzureResourceManager'
SubscriptionId = 'your-subscription-id'
SubscriptionName = 'Your Subscription'
AuthenticationMethod = 'ServicePrincipal'
ServicePrincipalId = 'your-sp-id'
ServicePrincipalKey = 'your-sp-secret'
TenantId = 'your-tenant-id'
DependsOn = '[AzDoProject]SecureProject'
}
# Create variable group linked to Key Vault
AzDoVariableGroup 'SecureVariables' {
Ensure = 'Present'
VariableGroupName = 'Secure Variables'
ProjectName = 'SecureProject'
KeyVaultName = 'company-keyvault'
ServiceConnectionName = 'Azure KeyVault'
Variables = @{
'StorageAccountKey' = 'storage-account-key-secret'
'DatabasePassword' = 'db-password-secret'
'ApiToken' = 'api-token-secret'
}
DependsOn = '[AzDoServiceConnection]KeyVaultConnection'
}
}
}
VariableGroupWithKeyVault
Start-DscConfiguration -Path ./VariableGroupWithKeyVault -Wait -Verbose# Retrieve variable group information
$properties = @{
VariableGroupName = 'App Settings'
ProjectName = 'MyProject'
}
$result = Invoke-DscResource -Name 'AzDoVariableGroup' `
-Method Get `
-Property $properties `
-ModuleName 'AzureDevOpsDscNative'
Write-Host "Variable Group: $($result.VariableGroupName)"
Write-Host "ID: $($result.VariableGroupId)"
Write-Host "Variables: $(($result.Variables | ConvertTo-Json))"Configuration UpdateVariableGroup {
Import-DscResource -ModuleName 'AzureDevOpsDscNative'
Node localhost {
# Update existing variable group with new values
AzDoVariableGroup 'UpdateAppSettings' {
Ensure = 'Present'
VariableGroupName = 'App Settings'
ProjectName = 'MyProject'
Variables = @{
'Environment' = 'Production'
'LogLevel' = 'Error' # Changed from Information
'MaxConnections' = '200' # Changed from 100
'NewSetting' = 'NewValue' # Added new variable
}
}
}
}
UpdateVariableGroup
Start-DscConfiguration -Path ./UpdateVariableGroup -Wait -VerboseConfiguration RemoveVariableGroup {
Import-DscResource -ModuleName 'AzureDevOpsDscNative'
Node localhost {
AzDoVariableGroup 'RemoveOldGroup' {
Ensure = 'Absent'
VariableGroupName = 'Old Settings'
ProjectName = 'MyProject'
}
}
}
RemoveVariableGroup
Start-DscConfiguration -Path ./RemoveVariableGroup -Wait -Verbose# VariableGroupsConfig.psd1
@{
AllNodes = @(
@{
NodeName = 'localhost'
ProjectName = 'MyProject'
VariableGroups = @(
@{
Name = 'Build Settings'
Variables = @{
'BuildConfiguration' = 'Release'
'BuildPlatform' = 'Any CPU'
}
},
@{
Name = 'Test Settings'
Variables = @{
'TestFramework' = 'NUnit'
'CodeCoverage' = '80'
}
}
)
}
)
}
# Configuration.ps1
Configuration CreateVariableGroupsFromData {
param([hashtable]$ConfigurationData)
Import-DscResource -ModuleName 'AzureDevOpsDscNative'
Node $AllNodes.NodeName {
foreach ($varGroup in $Node.VariableGroups) {
AzDoVariableGroup "VarGroup_$($varGroup.Name)" {
Ensure = 'Present'
VariableGroupName = $varGroup.Name
ProjectName = $Node.ProjectName
Variables = $varGroup.Variables
}
}
}
}
$data = Import-PowerShellDataFile -Path VariableGroupsConfig.psd1
CreateVariableGroupsFromData -ConfigurationData $data- Variable names should be descriptive and follow your organization's naming standards
- Use PascalCase or UPPER_SNAKE_CASE for consistency
- Avoid special characters except underscores
When linking to Key Vault:
- The service connection must have appropriate permissions
- Secrets stored in Key Vault will be masked in pipeline logs
- You can mix regular and secret variables
- Variables in a variable group can be used across multiple pipelines
- Project-level variable groups are accessible to all pipelines in that project
- Organization-level variable groups (using '@' as project name) are accessible organization-wide
To manage variable groups, ensure you have:
- Project-level administrator permissions, or
- Specific permissions for "Administer variable groups"
Cause: The specified project doesn't exist
Solution:
# Ensure the project is created first
DependsOn = '[AzDoProject]MyProject'Cause: A variable group with the same name already exists
Solution:
- Use a unique variable group name
- Or update the existing group instead of creating a new one
Cause: Service connection doesn't have permissions to Key Vault
Solution:
- Verify the service connection exists
- Ensure it has "Get" and "List" permissions on Key Vault secrets
- Check that Key Vault access policies allow the service principal
Cause: Variable group not linked to pipeline or permissions issue
Solution:
- Verify the variable group is in the same project as the pipeline
- Check pipeline YAML includes the variable group
- Verify user has read permissions on the variable group
- AzDoProject - Create and manage projects
- AzDoVariableGroupPermission - Manage variable group permissions
- AzDoServiceConnection - Create service connections
- AzDoPipeline - Create and manage pipelines