理解期望状态配置中的CONFIGURATION关键字 1


预期状态配置是Windows PowerShell中的一个新的管理平台,能让你在Windows上启用devOPs场景。它能使系统管理员和devOps声明式地配置系统,执行配置和重用配置。其核心方面就是针对Configuration关键字的语言扩展。
你可以参考下面一个来自MSDN的例子来体验如何使用configuration关键字配置机器。

Configuration MyWebConfig
 {
    # 一个配置语句块需要0个或者多个Node语句块。
    Node "Server001"
    {
       # 接下来,指定多个资源语句块。

       # WindowsFeature 是系统中内置的资源,可以在语句块中使用。
       # 下面的例子会确保IIs角色被安装。
       WindowsFeature MyRoleExample
       {
   	   # 要卸载角色, 可以将Ensure 设置为‘Absent’
           Ensure = "Present" 
           Name   = "Web-Server"  
       }

       # File 也是内置的资源用来管理文件和目录
       # 这个例子确保源目录的文件存在
       File MyFileExample
       {
   	  # 你也可以将 Ensure 设置为 "Absent"
          Ensure = "Present"  
   	  # 默认值为 “File”
          Type = "Directory" 
          Recurse = $true
          SourcePath = $WebsiteFilePath 
          DestinationPath = "C:\inetpub\wwwroot" 
  	  # 确保MyRoleExample提前执行成功
          DependsOn = "[WindowsFeature]MyRoleExample"        
 	}
    }
 }

从上面的例子可以发现我们使用的一个关键字configuration,用来声明式的定义配置。同时,这里在configuration内部,还有一个关键字Node和一些像关键字一样的资源集合。你可以通过Get-DscResource命令来列出这些资源和它们的属性。下面是我在Windows 8.1上面运行的输出。

PS > Get-DscResource 
ImplementedAs Name           Module                      Properties
------------- ----           ------                      ----------
Binary        File                                       {DestinationPath, Attributes, Checksum, Contents...}
PowerShell    Archive        PSDesiredStateConfiguration {Destination, Path, Checksum, DependsOn...}
PowerShell    Environment    PSDesiredStateConfiguration {Name, DependsOn, Ensure, Path...}
PowerShell    Group          PSDesiredStateConfiguration {GroupName, Credential, DependsOn, Description...}
Binary        Log            PSDesiredStateConfiguration {Message, DependsOn}
PowerShell    Package        PSDesiredStateConfiguration {Name, Path, ProductId, Arguments...}
PowerShell    Registry       PSDesiredStateConfiguration {Key, ValueName, DependsOn, Ensure...}
PowerShell    Script         PSDesiredStateConfiguration {GetScript, SetScript, TestScript, Credential...}
PowerShell    Service        PSDesiredStateConfiguration {Name, BuiltInAccount, Credential, DependsOn...}
PowerShell    User           PSDesiredStateConfiguration {UserName, DependsOn, Description, Disabled...}
PowerShell    WindowsFeature PSDesiredStateConfiguration {Name, Credential, DependsOn, Ensure...}
PowerShell    WindowsProcess PSDesiredStateConfiguration {Arguments, Path, Credential, DependsOn...}

Configuration是一个简单命名的关键字,那就意味着你可以使用字符串常量或者变量作为它的名称。它是期望状态配置中默认引进的唯一的一个关键字。它的语法看起来像一个函数,也就意味着它在一些限制下可以像语句块那样被执行。你可以指定一个参数语句块,或者一个处理语句块。整体来看Configuration语法像一个函数的定义。确实如此,它创建了一个被命名的命令。你执行它时,只须指定它的名称,就像执行invoke import-module .\sample.psm1一样,你可以使用get-command输出和查看这些新配置。

PS> Get-Command -CommandType Configuration

CommandType     Name                                               ModuleName
-----------     ----                                               ----------
Configuration   MyWebConfig
PS > Get-Help MyWebConfig

NAME
    MyWebConfig

SYNTAX
    MyWebConfig [[-InstanceName] ] [[-OutputPath] ] [[-ConfigurationData] ]

ALIASES
    None

REMARKS
    None

完了后,你可以像执行其它命令一样,通过键入MyWebConfig来执行MyWebConfig。这条命令会生成一个MOF文件,被发送到目标机器去配置。MOF是一个基于通用信息模型(CIM)标准的分布式管理任务。

PS C:\WINDOWS\system32> MyWebConfig

    Directory: C:\WINDOWS\system32\MyWebConfig

Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---        2013/12/26     16:47       1954 Server001.mof

至此,我们对于这个新的关键字Configuration有所认识了。接下来让我们来消化Configuration体,来认识在配置体内部可以指定什么?PowerShell 默认会在Configuration语句块内部添加两个关键字:Node 和 Import-DscResource。Node顾名思义,代表的是你要配置的机器。你可以在每个Node内部指定一系列的资源。每一个指定资源的开始为资源名称,并且包含了代表配置内容的一个哈希表,就像我们开头演示的例子中一样。更多关于DSC的信息,你可以访问technet网站

我们可以在机器上通过Get-DscResource命令来查看一系列可用的资源。这些资源也就是你当前在node语句块内部可以使用的资源。然后这里可能会有一个小误解,用户需要意识到。PowerShell会自动加载这些位于系统路径下,例如$pshome\modules 或者 $env:systemRoot\system32\configuration 的资源作为关键字。那如果它位于$env:PSModulePath路径,而非系统路径,这时我们需要显式的使用Import-DscResource命令导入这些资源让PowerShell 解释器能够理解它。我们可以使用下面的命令查看一个资源的详细信息。

PS C:\> Get-DscResource -name file | fl

ResourceType  : MSFT_FileDirectoryConfiguration
Name          : File
FriendlyName  : File
Module        :
Path          :
ParentPath    : C:\WINDOWS\system32\Configuration\Schema\MSFT_FileDirectoryConfiguration
ImplementedAs : Binary
CompanyName   :
Properties    : {DestinationPath, Attributes, Checksum, Contents...}

所有的资源会以动态关键字的形式被加载,这次我们没有足够的时间去探讨,希望在后面的文章中讲解。

除了期望状态配置(DSC)提供的资源以外,用户还可以构建自定义的资源。在TechNet上有个很好的文档来说明。该文档主要讨论PowerShell 基于资源的组件。用户实际上使用configuration关键字本身就可以添加一个资源。下面给出个例子:

configuration  scriptdsc
 { 
     param ($one, $two)
     Log scriptDscLogResource
     {
         Message = "This is the message from the embedded log resource: one=$one two=$two three=$three"
     }

       File MyFileExample
       {
          Ensure = "Present"  
          Type = "Directory“ # Default is “File”
         Recurse = $true
          SourcePath = $one # This is a path that has web files
          DestinationPath = $two # The path where we want to ensure the web files are present
       }
 }

Export-ModuleMember -Function scriptdsc

这个文件已经被命名为resourceName.Schema.psm1,和一个对应的resourceName.psd1文件。然后,你可以把它作为其它PowerShell资源来部署它。这个能力能让用户像下面一样编写配置和重复利用别人编写好的配置。

Configuration MyConfig
 {
    Node "Server001"
    {
       scriptdsc MyExample
       {
           One = "\\share\file.txt" 
           Two = "d:\file.txt"  
       }
    }
 }

在这篇文章中,我们对configuration关键字进行了简要介绍。在后续文章中,我会讨论更多关于使用配置数据来自定义配置,在配置中嵌入指定的对象和$using变量。因为很多人感兴趣,我们也会消化动态关键字内部的实现。

原文作者:Joe Zhou (微软 SDE)

原文链接:Understanding CONFIGURATION keyword in Desired State Configuration

本文链接: https://www.pstips.net/understanding-configuration-keyword-in-dsc.html
请尊重原作者和编辑的辛勤劳动,欢迎转载,并注明出处!

关于 Mooser Lee

我是一个Powershell的爱好者,创建了PowerShell中文博客,热衷于Powershell技术的搜集和分享。本站部分内容来源于互联网,不足之处敬请谅解,并欢迎您批评指正。

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注

一条评论 “理解期望状态配置中的CONFIGURATION关键字