Jump to content

Software Installation Using Machine Configuration and Azure Policy


Recommended Posts

Guest Anthony_W
Posted

I did a post a while ago on installing software onto virtual machines using policy state change events as the trigger. Now with the general availability of Azure Automanage Machine Configuration (formerly Azure Policy Guest Configuration) it’s time for a bit of an update to that post. In this guide I’ll again be installing PowerShell 7 – however I’ll use Machine Configuration and Azure Policy to handle the installation. Let’s get started….

 

 

 

Development Environment

 

 

To make sure I have all the tools to complete this process I need to install some pre-requisite software. This is so I can generate the package which the virtual machine will download to tell it how to install the software.

 

On my local machine I have installed:

 

  • PowerShell 7.3.0
  • The GuestConfiguration module – version 4.2.0
  • The PSDscResources module – version 2.12.0.0
  • An Azure Storage Account with a container which will host my configuration. The virtual machine needs to be able to contact this Storage Account so make sure it has connectivity.

 

The next steps show the process I use to create the configuration right through to running the remediation. I’ve uploaded the script to GitHub so you can follow it through and see what I have done.

 

 

 

Create the Configuration and MOF File

 

 

First step is to write a DSC configuration to install the software. I’m using the built in MSIPackage resource because it allows me to specify a URL which the software can be downloaded from. Note that DSC in Machine Configuration works a little bit differently – you can’t have credentials or reboots so be aware of those limitations. My configuration looks like below.

 

largevv2px999.png.cdf66c7f28623e61d1e87921a9e00f7e.png

 

To generate the MOF file I just run the configuration by calling it: -

 

largevv2px999.png.18384a0649ab8165d5be9c822a98c6e5.png

 

A MOF file is generated – the same as a normal DSC configuration and you can have a look to see what it contains.

 

 

 

Create the Configuration Package Artifacts

 

 

Now I use the GuestConfiguration module to create an artifact. This command packages together my MOF file, plus all the required modules into a zip file.

 

largevv2px999.png.c6d065870b5b449973f8ab3e1e81a1b9.png

 

I just use the generated MOF file as an input and it will create the zip file for me. Notice that the type is ‘AuditAndSet’. This is telling the configuration that it can make changes to the virtual machine. The other option is ‘Audit’ which just checks the setting and reports the compliance state.

 

 

 

Upload the Package to a Storage Account

 

 

I must put the zip file into a storage account – I also need to generate a SAS URL which is going to be embedded into the policy. This is so the Machine Configuration agent knows where to get the package from.

 

largevv2px999.png.00bacfb430e7830a0057f733d4cf2468.png

 

 

 

Azure Policy Creation

 

 

All that is left to do is create the Azure Policy object which will check compliance for me – and allow me to commence remediation. The GuestConfiguration module has a command to generate this for me as demonstrated below.

 

largevv2px999.png.4ae27c1aa873ae5c37d46a6d738ec4ff.png

 

A couple of things to note about some of the parameters in the image above:-

 

  • PolicyID is a unique ID I generated using the New-Guid cmdlet – if you make changes or updates to the policy ensure this remains the same.
  • Platform - ‘Windows’ in this case – if you want to do installs on Linux you need to write class-based DSC resources to do this (will cover in a future post).
  • Mode is ‘ApplyAndAutoCorrect’ – this will configure the machine and correct drift as well.
  • Tag – allows me to have some control over how the installation is targeted. This policy will only apply to machines which have a tag called “InstallPowerShell” with the value set to true.

 

The last line of the code above takes the generated policy and uploads it to Azure – by default it uploads it to the subscription but you can use any method you want to create the definition.

 

 

 

Assign the Policy

 

 

Before we assign the machine configuration policy we need to ensure that the prerequisite policy is installed – this is the same as in the previous blog.

 

largevv2px999.png.edab5d8cead014ee949aa0f9d95a6d16.png

 

The machines need to have a Managed Identity enabled so they can authenticate to the GuestConfiguration resource provider.

 

Assign the newly created policy to a scope of your choosing – we are now ready to test.

 

 

 

Testing the Deployment

 

 

I have my policy assigned to a resource group – the effect is going to be DeployIfNotExists however this works a little bit differently to a normal DINE policy. To show you how this works – I’ve assigned my policy to an empty resource group as below.

 

largevv2px999.png.f0667dde7a33f77c0da48c413782463f.png

 

As expected I can see 100% compliance because there is nothing in that resource group.

 

largevv2px999.png.d30e9f11f51488965a2262cf3fdbd0da.png

 

Now I’m going to build a virtual machine in that resource group and wait for policy to take effect on it. The prerequisite policies set above will go through and enable a system assigned Managed Identity and install the policy agent for me.

 

 

 

My compliance is still showing 100% after this is done and this is the first trick we can use to control how this is deployed. Remember I specified a tag in my policy assignment – in order to make this policy apply to that virtual machine I need to add the tag to the server.

 

largevv2px999.png.0d2f6f4aa95ffe64b08c08ae79a01fb6.png

 

When I do that a sequence of events start – because the resource has been updated eventually the DeployIfNotExists policy will evaluate. However, unlike a normal DINE policy what Machine Configuration does is uses the metadata in the policy definition to create a Guest Assignment resource. The most important field on this object is the “assignmentType” and it is set to null by default. This means that the virtual machine will start evaluating the assignment but will only work in an audit mode.

 

 

 

Here is what the policy metadata looks like: -

 

largevv2px999.png.839d1efc94d93eb6c4889d2fae0b3581.png

 

And if I look at the Guest Assignment object in Azure (using the Resource Graph so we can view the properties) you can see that the assignmentType is null.

 

largevv2px999.png.122f9234c8af0aaafb6458bcdffb2f91.png

 

At this point the virtual machine will have received the assignment and downloaded the package from the storage account – and evaluated its compliance. It reports this back to the Guest Assignment object which Azure Policy is monitoring, and we can now see the non-compliant resource in the policy view.

 

largevv2px999.png.344ed3b5a4cec99b62f538b4e7352b82.png

 

And here is the compliance reason: -

 

largevv2px999.png.60f20d4503c487b0fa79dd545f3d57ed.png

 

The second “gate” we have to control the deployment is to run the proper DeployIfNotExists effect on the deployment and I can do this using a remediation task.

 

 

 

This will cause the deployment to happen as stated in the policy file – and you can see the major difference is that it will set that assignmentType value to “ApplyAndAutoCorrect”.

 

largevv2px999.png.991137ae0f1b2b61f0f26855ed2512d5.png

 

After that remediation task is run – I can check the Guest Assignment object in the Resource Graph and notice the difference.

 

largevv2px999.png.0d7193477f5564f97dea2d9128421bd3.png

 

The virtual machine will now download the new assignment details and see that the assignmentType has changed – the next time it runs it will now perform the correction if required. Once the software is installed the server will send a report back to the Guest Assignment service – and this will update the Guest Assignment object to compliant. Azure Policy will then check that object and report back the overall compliance.

 

 

 

Here is what the Guest Assignment resource looks like: -

 

largevv2px999.png.cfd8c3d4c1c9048f9241df4c0f398851.png

 

And finally, the policy will also be compliant.

 

largevv2px999.png.bd718ffb57933b241ed3acc557ef3328.png

 

There are definitely a lot of moving parts involved with Machine Configuration and some limitations at this stage – however it is the way of the future so jump in and check it out.

 

 

 

Disclaimer

 

The sample scripts are not supported under any Microsoft standard support program or service. The sample scripts are provided AS IS without warranty of any kind. Microsoft further disclaims all implied warranties including, without limitation, any implied warranties of merchantability or of fitness for a particular purpose. The entire risk arising out of the use or performance of the sample scripts and documentation remains with you. In no event shall Microsoft, its authors, or anyone else involved in the creation, production, or delivery of the scripts be liable for any damages whatsoever (including, without limitation, damages for loss of business profits, business interruption, loss of business information, or other pecuniary loss) arising out of the use of or inability to use the sample scripts or documentation, even if Microsoft has been advised of the possibility of such damages.

 

Continue reading...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...