Hello, Cloud Gurus,
and welcome back for Part 1 of our two-part lesson
where we're going to take a look
at implementing Desired State Configuration,
more commonly referred to as DSC, with Azure Automation.
Let's take a look at what we're gonna cover.
First up, we'll do a bit of an introduction
to Desired State Configuration and more specifically,
how it works with Azure's automation service.
And then we'll take a look at creating configurations,
and then we'll wrap up by taking you through a demo
of creating a configuration.
I'm really looking forward to this. Let's get started.
In the first lesson in this section,
we spoke about desired state
and how you want your service to look
and act in a particular way,
and that means you want them to be configured
to a desired state.
Now, you can achieve this with Desired State Configuration
and there are a few versions of DSC,
and in this lesson, we will focus on the oldest
and most mature implementation of DSC,
which is part of Windows PowerShell 5.1.
And Microsoft refers to this version of DSC
as State Configuration in the Azure Portal.
Desired State Configuration works
by firstly defining your desired state.
This is done by declaring the resources you want configured
and how you want them to be configured.
One example might be a Windows Server role,
like the Active Directory role.
That's the resource.
And you might want it to be installed
and that's the state that you want that role to be in.
Another example might be a primary DNS server
configured on a network adapter
and you might want it to be 10.1.1.5.
As you can see, we're declaring system configuration values
that we want our servers to have.
And once you've done this,
you'll have what's referred to as a configuration
and all of its included resources declared
and you'll notice I keep using the word declared,
and that is intentional.
When you declare something, you're using what's referred to
as a declarative programming language.
A declarative language states how things should be,
not exactly how to make it so.
You then need to compile the configuration
into Microsoft's Managed Object format or MOF format,
which is a carryover
from the Windows Management Instrumentation.
The MOF files are text files,
but they're not really designed to be human readable.
Then you need to get your servers
to apply the configuration.
In Windows PowerShell 5.1, this is the responsibility
of the Local Configuration Manager or LCM.
Now, you can push your configuration to the server,
which means you tell the LCM
to apply a provided configuration, but in Azure Automation,
you can have the LCM pull the configuration
from what's referred to as a pull server.
Azure Automation is a Microsoft managed pull server.
The responsibility of compiling the configuration
moves from your administrator PC
to the Azure Automation account.
You publish the configuration
and Azure Automation compiles it to a MOF file.
You tell the server where to find a configuration
by registering the server with the Azure Automation account,
and you also tell it how to apply the configuration.
The LCM will then pull the configuration and apply it
and the configuration and the resources it uses
are designed to be applied over and over again
to achieve the same result.
And this is referred to as idempotency.
Let's take a look at each of these steps in more detail.
Before you can create a configuration,
you need PowerShell modules.
A configuration contains the desired state,
but the modules and the resources in those modules
contain the logic to bring the server
into the desired state,
and you can run the Get-DscResource PowerShell cmdlet
to see a list of DSC resources for the modules
that you have installed.
To create a configuration, you create a PowerShell file
and declare a configuration by giving it a name.
In this example, I've created a configuration
with the name, ServerConfiguration.
Then you import the required resources from the modules
with the Import-DscResource PowerShell cmdlet.
Then you define which servers the configuration applies to
with the node block
and a configuration can have one or more node blocks.
Next, you define the resources
and how you want those resources to be configured.
In this example, I'm defining an archive resource
with the name, UnzipApp
and I'm telling the archive resource
that I want a zip file unzipped
to the C:\Temp\App directory.
And you can also control the order
of how resources are applied using DependsOn.
In this example, I'm telling the package resource
to wait for the unzip before attempting to install the app.
And now you can compile the configuration locally
to a MOF file.
But when you're using DSC with Azure Automation,
the automation account does the compiling for you.
So the next step is to push the configuration
to Azure Automation and request the automation account
compile the configuration.
And we'll take a look at that in the demo.
All right, let's take a look at a demo
of creating a configuration
and pushing the configuration to Azure Automation
so it can be compiled.
In this demo, we will create a configuration
to deploy Windows Admin Center,
and then we'll reconfigure
our Windows Admin Center Server using DSC.
In the demo, we'll compose a configuration
using Visual Studio Code
and then we'll import the configuration
into our Azure Automation account, and to finish up,
we will request the Azure Automation Account
compile the configuration.
Let's take a look.
All right, so here we are in Visual Studio Code
and I've gone ahead and installed
the Azure PowerShell module and the PowerShell extension
for Visual Studio Code
and I've also connected Azure account in PowerShell
with the Connect-AzAccount PowerShell cmdlet.
Let's go ahead and create a new PowerShell file.
So we'll just go file and then new file.
And then we'll select PowerShell as the language.
Now, Microsoft includes code snippets that you can use
to easily create Desired State Configuration configurations,
so let's go ahead and use a code snippet
to get a general structure of a configuration
and then we'll tweak it to meet our requirements.
So I'll start by using
the example DSC configuration code snippet.
Go ahead and select that.
And now let's go ahead and change this configuration
to meet our requirements.
So I'm going to change the name of the configuration
to server configuration,
and you can use any name that you want.
Just make sure that you save the file
with the same name that you use for the configuration name,
because Azure Automation requires this.
And we'll go ahead and remove the comments and examples,
because we don't need those.
And now we'll go ahead and change the node name
to our Windows Admin Center node.
So we'll just change the node name to CORP-BNE-WAC01
and we can go ahead and remove the other nodes
and we're not going to use these resources,
so we'll go ahead and delete all these resources as well.
All right.
Now, we're going to need some resources
that aren't part of the default modules
included with PowerShell,
so we're going to install
the xPSDesiredStateConfiguration module,
which is a powerful community-backed
Desired State Configuration PowerShell module.
So to do that, we'll run
Install-Module xPSDesiredStateConfiguration
and we'll use the force parameter to suppress any prompts.
This will just take a moment to install,
so we'll come back when this is done.
All right, the xPSDesiredStateConfiguration module
is now installed.
Now let's go ahead and create our configuration.
To do that, we're going to need the resources
from the xPSDesiredStateConfiguration module,
so we're going to import that into our configuration
with the Import-DscResource PowerShell cmdlet.
So we'll go ahead and add that to our configuration.
And now let's take a look at the resources that are included
in the xPSDesiredStateConfiguration module.
To do that, we're going to run Get-DscResource
and then we're gonna specify the module name
of xPSDesiredStateConfiguration.
All right and the formatting's a little odd here
in Visual Studio Code,
but we can see that there is a number of resources
included in this module.
The two that we are interested in
to download and install Windows Admin Center
are xRemoteFile, which is this one here,
and xPackage which you can use to install software.
Now let's take a look at the syntax that we need to use
with the resource xRemote file,
so I'm going to go ahead and clear the screen here,
because this is getting a little bit messy
and we'll run Get-DscResource again
and we'll specify the name of the resource
and then we'll specify that we want to see the syntax.
All right, formatting's a little off again here,
but you can see that we've got the basic syntax
for the xRemote file DscResource,
so I'm going to go ahead and create the configuration
using both the xRemote file DscResource
and the xPackage DscResource
and then we'll come back when I'm done.
All right, so I've gone ahead
and finished creating the configuration here
and I didn't want you to have to sit through that,
because we'd spend the entire remainder of this lesson
watching me fumble my way through this code.
But anyway, let's go through the important parts.
So we're downloading our file
and we're downloading the file from aka.ms/WACDownload
and we're storing it in C:\Temp
and because of the DependsOn here
where we're saying wait for the xRemote file resource
with the name DownloadWAC,
it will then install Windows Admin Center
and it's going to install it from C:\Temp\WAC.msi.
It's going to use these installer command-line arguments
and it's going to look
for this MSI product ID once it's installed
to confirm that the product has been installed successfully.
All right, now that we have our configuration created,
let's go ahead and save our configuration
and then we'll import it into our automation account.
So I'll go ahead and save this now.
And we need to make sure we use
the same name as our configuration
because Azure Automation requires that.
I'll go ahead and click Save.
And now we're going to import the configuration
into our automation account using the,
Import-AzAutomationDscConfiguration PowerShell cmdlet.
We'll specify the automation account name,
the resource group name and the source path
for our configuration, which is this PowerShell file,
and we also need to specify
that the configuration is published.
We can go ahead and run that command now.
All right, the next step is to compile our configuration
and the Azure Automation account
is going to take care of that for us,
but before it can compile the configuration,
we need to import the required PowerShell modules
for our DSC resources into the automation account.
So let's jump over to the Azure portal
and we'll import a shared module
that can be used to compile this configuration.
So here I am in the automation account
that we've just imported our configuration into,
and I'm going to navigate down to Shared Resources
and then Modules,
and then we'll select to Add a Module
and we'll go ahead here and browse the gallery.
And we'll paste in our module name.
And then we'll go ahead and select that module.
And then we'll go ahead and hit Select
and then we'll select to import it for PowerShell 5.1.
And then we'll go ahead and hit Import.
Now, this will take a moment to import,
so we'll come back when this is done.
All right, that PowerShell module has been imported
into our automation account.
Now, let's jump back to VS Code
and we can start a compilation job
in our automation account.
So here we are back in VS Code.
Let's go ahead and start a compilation job
using the Start-AzAutomationDscCompilationJob
PowerShell cmdlet.
We'll specify the configuration name,
the automation account name, and the resource group name.
We'll go ahead and run that command.
All right and that compilation job is started,
let's jump back to our automation account
and take a look at the compiled configuration.
So here we are in our automation account
and we'll navigate to state configuration
under configuration management
and then we'll navigate into configurations.
And we have our imported server configuration here,
but we have no compiled configurations yet,
so we'll select our configuration
and we can see our compilation job is starting here.
This will take a moment to complete,
so we'll come back when this is done.
All right, our configuration is compiled successfully.
Let's take a look at the node configurations
that have been generated from this configuration,
so we'll select Node Configurations.
And we can see we have one node configuration,
which is server configuration,
a dot and then our server name.
We're going to need this name for later,
so we'll take note of this.
That wraps up this demo.
Let's take a look at what we've covered in this lesson.
All right, so what have we covered?
A configuration is made up of nodes,
the resources to be configured on those nodes
and the properties of those resources.
And you can import resources into your configuration
using the Import-DscResource PowerShell cmdlet.
And you can use the DependsOn attribute of a resource
to control the order in which that resource is applied
in the configuration.
And the node block in a configuration declares
which servers you want the configuration to apply to
and when you're using Desired State Configuration
with Azure Automation,
the Azure automation account is responsible
for compiling the configuration.
All right, thanks for being with me
in part one of this lesson,
I hope you found it useful
and I'll look forward to seeing you in part two.