Configuring Azure VMs using Desired State Configuration

Lately I've been working on a project to automate the provisioning of virtual machines on Azure using Azure Resource Manager. The need to apply configuration on the OS level came up and the proffered way was Desired State Configuration. On this article, we are going to publish a configuration and configure a virtual machine to apply it.

First we are going to log in and select the subscription that we are going to use:

PS C:\>Login-AzureRmAccount

Account          : c******s.p******ou@*******
SubscriptionName : Pay-As-You-Go
SubscriptionId   : e******a-8**c-4**3-9**7-b**********9
TenantId         : 5******f-d**2-4**4-a**e-7**********7
Environment      : AzureCloud

PS C:\>Get-AzureRmSubscription |
           Out-GridView -PassThru -Title "Select the subscription to use" |
               Select-AzureRmSubscription

Name             : [c*****s.p******ou@*******, e*******a-8**c-4**3-9**7-b**********9]
Account          : c******s.p********u@********
SubscriptionName : Pay-As-You-Go
TenantId         : 5*******f-d***2-4**4-a**e-7*********7
Environment      : AzureCloud

PS C:\>

I'm very fond of resource groups so I'll create one to group the DSC resources:

PS C:\> New-AzureRmResourceGroup -Name "Blog-DSC" -Location WestEurope

ResourceGroupName : Blog-DSC
Location          : westeurope
ProvisioningState : Succeeded
Tags              :
ResourceId        : /subscriptions/********/resourceGroups/Blog-DSC

PS C:\>

We are going to need a storage account to host the configuration so let's create one:

PS C:\> New-AzureRmStorageAccount -ResourceGroupName "Blog-DSC" `
                                  -Name "blogdscstorage" `
                                  -Location WestEurope `
                                  -SkuName Standard_LRS `
                                  -Kind BlobStorage `
                                  -AccessTier Cool

ResourceGroupName  : Blog-DSC
StorageAccountName : blogdscstorage
Id                 : /subscriptions/********/..../storageAccounts/blogdscstorage
Location           : westeurope
Sku                : Microsoft.Azure.Management.Storage.Models.Sku
Kind               : BlobStorage
AccessTier         : Cool

PS C:\>

Perfect! Now let's create a DSC configuration file.

We are going to make this simple, the configuration will contain only one directive, to create the folder "C:\Temp".

Configuration TestConfig
{
    Node "localhost"
    {
        File TempDirectory
        {
            Ensure = "Present"
            Type = "Directory"
            DestinationPath =  "C:\Temp"
        }

        Log TempDirectoryLog
        {
            Message = "Finished running the file resource with ID TempDirectory"
            DependsOn = "[File]TempDirectory"
        }
    }
}

To publish the configuration, use the Publish-AzureRmVmDscConfiguration cmdlet:

PS C:\> Publish-AzureRmVMDscConfiguration -ConfigurationPath .\TestConfig.ps1 `
                                          -ResourceGroupName "Blog-DSC" `
                                          -StorageAccountName "blogdscstorage" `
                                          -Force
https://blogdscstorage.blob.core.windows.net/windows-powershell-dsc/TestConfig.ps1.zip

PS C:\>

If everything worked, you should have a container named "windows-powershell-dsc" that contains the archived configuration file as shown below:


Now that the configuration is published, you can update your virtual machines with the DSC extension and have them apply the configuration:

PS C:\> Set-AzureRmVMDscExtension -ResourceGroupName "Blog-DSC-VMs" `
                                  -VMName "DSC-Test-001" `
                                  -ArchiveBlobName "TestConfig.ps1.zip" `
                                  -ArchiveStorageAccountName "blogdscstorage" `
                                  -ArchiveResourceGroupName "Blog-DSC" `
                                  -ConfigurationName "TestConfig" `
                                  -AutoUpdate:$true `
                                  -Version 2.72

PS C:\>

The configuration should have been applied to the virtual machine:

C:\> Get-DscConfiguration

ConfigurationName : TestConfig
DependsOn         :
ResourceId        : [File]TempDirectory

ConfigurationName : TestConfig
DependsOn         : {[File]TempDirectory}
ResourceId        : [Log]TempDirectoryLog

C:\>

This is the simplest approach to follow when you want to apply a configuration to machines. On the next article, we'll see how we can take advantage of the Azure Automation DSC service to apply configurations using an Automation Account and report on the status of the machines.

Popular posts from this blog

Domain Controller Machine Password Reset

Configuring a Certificate on Exchange Receive Connector

Running Multiple NGINX Ingress Controllers in AKS