これは、アドベントカレンダー9日目の記事です。
さて、9日目ったら9日目です。
今日はリソース(Resource) の配置です。標準のリソース以外の操作をしたくなったらこれですね! では見てみましょう。
リソースってなんだっけ
覚えてますか? 3日目でChefと比較しましたね。
対象 | DSC | Chef | 概要 |
---|---|---|---|
どうやるか (ロジック) |
リソース (Resource) |
クックブック (Cookbook) |
どのような状態であるべきか |
何をどのような状態に (呼出し) |
コンフィグレーション (Configuration) |
レシピ (Recipe) |
ロジックを呼び出してあるべき状態を宣言型構文で指示 |
- コンフィグレーションがどのような状態にするかを宣言的に記述したもの
- リソースは、その状態に至るロジック
リソースがあれば、それに対応した状態をコンフィグレーションで記述できる => やれることが増える! です。はい。
リソースは公開されています
別にリソースは自分で書かなくてはいけないものではなく、オープンソースに公開されたものが数多くあります。 有名どころを挙げておきましょう。
オーナー | 公開元 | URL |
---|---|---|
Microsoft | TechNetScriptCenter(Script Browser & Script Analyzer) | https://gallery.technet.microsoft.com/scriptcenter/DSC-Resource-Kit-All-c449312d |
Micorsoft | GitHub | https://github.com/PowerShell |
Microsoft | PowerShellGet(PowerShell Gallery) | https://www.powershellgallery.com/ |
PowerShellOrg | GitHub | https://github.com/powershellorg/dsc |
guitarrapc | GitHub | https://github.com/guitarrapc/DSCResources |
順にみておきましょうか。
TechNetScriptCenter (Script Browser & Script Analyzer)
以前も紹介した、Microsoftのスクリプト公開スペースです。数年前からあるもので、すでに多くのPowerShellのスクリプトベースが公開されています。
https://gallery.technet.microsoft.com/scriptcenter/DSC-Resource-Kit-All-c449312d
URLからのアクセスになり、体系的にまとまっているわけでもなくとてつもなく使いにくいです。
2013年にScript Browser & Script Analyzerが公開されてからは、PowerShell ISEから検索、インストールが可能になり良くなりましたがしかしないですねぇ。
https://www.microsoft.com/ja-JP/download/details.aspx?id=42525
PowerShell Team - GitHub
2015年4月から、PowerShell TeamはGitHubでOSSにコードを公開始めました。その中にPowerShell TeamがTechNetで公開していたDSC Resourceも含まれています。
詳細はこっちの記事で!
GitHubに公開されたことで、 Submoduleでの参照などコードベースの組み込みがしやすくなりました。
しかし、一般に導入するには利用しやすいかというと、あと一歩感があります。
PowerShellOrg - GitHub
PowerShell.OrgもカスタムリソースをGitHubで公開しています。OSSとしてはこっちがメジャーですね。
ちょっと質に問題があって微妙なリソースも多いのですが、IssueなりPull Requestしてください。
GraniResource - GitHub
私が所属する謎社で使っているリソースを公開しています。
謎社で書いたリソースがGraniResoureです。実際本番環境で安定して利用しているリソースであり、基本的にはベストプラクティスとテストを通過したものばかりなので、ぜひ利用してみていただけると。
一部PowerShell Teamのリソースで使いにくいところを修正したりしたカスタムリソースもあります。
PowerShellGet
DSC Resourceは、GitHub、Gistで公開されたものもあります。けどちょっと探しにくかったり利用しにくいかも。
探しにくいのは利用者からすると気づけかれないというサイテーであり、利用しにくいのはハードルになるでしょう。
そこで、MicrosoftのPowerShell Teamが主導してNuGetをベースにタグでPowerShellモジュール関連をapt-getライクに取得できるようにごにょごにょしているPowerShellGetがあります。
これを使うとPowerShell v5ではNuGetライクにモジュールがインストールできます。
検索もたったこれだけ。
Repository Version Name Description ---------- ------- ---- ----------- PSGallery 1.0.0.0 AppDomainConfig Manipulate AppDomain configuration for your current PowerShell sess... PSGallery 1.0 AutoBrowse AutoBrowse is a module that lets you automate browsing with Interne... PSGallery 1.1.0.0 AutoVars Allows for the definition of automatic (calculated) variables in yo... PSGallery 0.8 Await Await - A modern implementation of EXPECT for Windows. For a demo, ... PSGallery 2.0 BetterCredentials A (compatible) major upgrade for Get-Credential, including support ... PSGallery 5.0 Bing A few functions for working with the new Bing APIs PSGallery 1.0.0.1 cFileShare The cFileShare module is a part of the ALM Ranger DevOps solutions ... PSGallery 1.0.0.1 CimInventory Module that should help read data from CIM/WMI data from remote com... PSGallery 1.0 CodeCraft CodeCraft helps you crank out the code. PSGallery 0.0.0.4 ContosoAnalyzerRules Script analyzer rules for Contoso. PSGallery 6.0 ConversionModule a module that performs various unit conversions PSGallery 1.0.0.7 DebugPx The DebugPx module provides a set of commands that make it easier t... PSGallery 1.0.1 Discovery Open the Black Box PSGallery 1.0.0.8 DoPx The DoPx module provides a rich set of commands that extend the aut... PSGallery 1.0 EWS Module to enable automating mail-related tasks PSGallery 1.4 EZOut Taking the Fight out of PowerShell Formatting PSGallery 2.1.0.2 FAQ A Frequently Asked Questions (and answers) module as a demonstratio... PSGallery 1.0.0.0 FormatPx FormatPx separates the formatting layer from the data layer in Powe... PSGallery 0.2.1.0 Formulaic Because Math is Tedious, not Hard PSGallery 1.0.0.1 GenericMethods The Invoke-GenericMethod function allows you to call generic .NET m... PSGallery 0.4 GistProvider Gist-as-a-Package - OneGet PowerShell Provider to interop with Gith... PSGallery 1.2.0.0 HardwareManagement Out-of-band hardware management module PSGallery 1.0 Heatmap Heatmap is a module to help you see how hot your servers are running PSGallery 1.0.0.8 HistoryPx HistoryPx uses proxy commands to add extended history information t... PSGallery 1.0.1 HttpListener Creates a new HTTP Listener enabling a remote client to execute Pow... # 中略 PSGallery 1.0 Winformal Write winforms informally in scripts with WinFormal PSGallery 2.1 xActiveDirectory Module with DSC Resources for Active Directory PSGallery 0.1.0.0 xAdcsDeployment The xCertificateServices module can be used to install or uninstall... PSGallery 0.1.2 xAzure Module with DSC Resources for Azure Resources PSGallery 1.0.0.0 xBitlocker This DSC Module allows you to configure Bitlocker on a single disk,... PSGallery 0.1 XBPS Script Cmdlets to interact with the Xbox Debug APIs. PSGallery 1.0.0 xChrome Module for installing the chrome PSGallery 1.2.1 xComputerManagement The xComputerManagement module is a part of the Windows PowerShell ... PSGallery 1.0.1 xCredSSP Module with DSC Resources for WSMan CredSSP. PSGallery 1.1.2 xDatabase The xDatabase module is a part of the Windows PowerShell Desired St... PSGallery 1.1 xDhcpServer The xDhcpServer module is a part of the Windows PowerShell Desired ... PSGallery 1.0.0 xDismFeature Module with DSC Resources for Deployment Image Servicing and Manage... PSGallery 1.0 xDnsServer The xDnsServer module is a part of the Windows PowerShell Desired S... PSGallery 2.0 xDscDiagnostics Module to help in reading details from DSC events PSGallery 1.1.1.1 xDSCResourceDesigner The xDscResourceDesigner module is a part of the Windows PowerShell... PSGallery 1.0.1.0 xExchange This DSC Module allows you to configure many different properties o... PSGallery 2.6.0.0 xEXOUserAvailability xEXOUserAvailability can help you to get user availability from Off... PSGallery 1.1.1 xFailOverCluster The xFailOverCluster module is a part of the Windows PowerShell Des... PSGallery 1.0.0 xFirefox Firefox Main module PSGallery 2.1.1 xHyper-V The xHyper-V module is a part of the Windows PowerShell Desired Sta... PSGallery 1.0.3 xHyper-VBackup To be utilized with Windows Server Technical Preview for the purpos... PSGallery 0.2.16.2 xJea Module with DSC Resources for Just Enough Admin (JEA). Jea makes i... PSGallery 1.0.0.0 xMySql The xMySql module is a part of the Windows PowerShell Desired State... PSGallery 2.1.1.1 xNetworking Module with DSC Resources for Networking area PSGallery 1.0.0 xOneGet DSC Resource for OneGet PSGallery 0.1.0.1 xPendingReboot This module identifies pending reboots in Windows Server and acts o... PSGallery 1.0.1 xPhp The xPhp module is a part of the Windows PowerShell Desired State C... PSGallery 3.0.3.3 xPSDesiredStateConfiguration The xPSDesiredStateConfiguration module is a part of the Windows Po... PSGallery 1.0.2 xRemoteDesktopAdmin Module with DSC Resources for enabling adminsitrative Remote Deskto... PSGallery 1.0.1 xRemoteDesktopSessionHost The xRemoteDesktopSessionHost module is a part of the Windows Power... PSGallery 1.0.0.1 xSafeHarbor Module containing example of creating safe environment using DSC an... PSGallery 1.1.0.0 xSCDPM Module with DSC Resources for deployment and configuration of Micro... PSGallery 1.1.0.0 xSCOM Module with DSC Resources for deployment and configuration of Micro... PSGallery 1.1.0.0 xSCSMA Module with DSC Resources for deployment and configuration of Micro... PSGallery 1.1.0.0 xSCSPF Module with DSC Resources for deployment and configuration of Micro... PSGallery 1.1.0.0 xSCSR Module with DSC Resources for deployment and configuration of Micro... PSGallery 1.1.0.0 xSCVMM Module with DSC Resources for deployment and configuration of Micro... PSGallery 1.0.1 xSmbShare The xSmbShare module is a part of the Windows PowerShell Desired St... PSGallery 1.1.3.1 xSqlPs SQL module. PSGallery 1.1.0.0 xSQLServer Module with DSC Resources for deployment and configuration of Micro... PSGallery 1.0.1 xSystemSecurity The xSystemSecurity module is a part of the Windows PowerShell Desi... PSGallery 1.3.2.2 xWebAdministration Module with DSC Resources for Web Administration PSGallery 1.0 xWindowsUpdate Module with DSC Resources for Windows Update PSGallery 0.0.0.1 xWinEventLog Configure Windows Event Logs PSGallery 1.0.0.0 xWordPress This module contains the xWordPressSite and xIisWordPressSite resou...
ね? 便利。紹介を別途していますのでどぞどぞ。
今回のサンプル
今回はDSCResourceを使って紹介します。
リソースの配置場所
DSCリソースの配置は、システムレベルのみとなります。リソースを設置するパスはこれだけです。
C:\Program Files\WindowsPowerShell\Modules
PowerShell 4.0において、DSCはx64のみの対応のため、C:\Program Files (x86)\WindowsPowerShell\Modules
は参照しません。
また、C:\Windows\System32\WindowsPowerShell\v1.0\Modules
にも置きませんし、C:\Windows\System32\WindowsPowerShell\v1.0\Modules
も参照しません。
リソースのダウンロード
今回は、GraniResourceを使うのでをクローンするか、Zipでダウンロードしてください。
ダウンロード解凍すると以下の構成になると思います。
ワンライナーでコピー
管理者で実行したPowerShellで一発コピーいいですね。
ダウンロードしたフォルダ直下で以下のワンラインーをPowerShell(管理者で起動)に入力すると、リソースがコピーされます。
マウスポチポチ
あるいは、マウスでポチポチなら以下のパスに移動して
DSCResources-master\Custom
GraniResourceフォルダを、C:\Program Files\WindowsPowerShell\Modules
にコピーします。
リソースが正しく設置できたか確認する
DSCリソースは、DSC用に拡張されたPowerShellモジュールです。
そのため、2つの確認ができます。
モジュールとして読み込めるか
主に作成した時に、モジュールの構成として正しいかを確認する時に使います。公開されているものを利用するときは余り使わない....です、たぶん、きっと。
- モジュールとしての構成に問題がないかの確認です
以下のコマンドがエラーなく完了すればokです。
バージョンも取れてますね。
ここまでが問題なければ、次にリソースとして読み込めるかを確認しましょう。
リソースとして読み込めるか
リソースを新たに追加したら必ず確認しましょう。
- リソースとしての構成に問題ないかの確認です
リソースの詳細構成は後日説明しますが、MOFファイルのスキーマ構成や、.psm1
の構成が正しくないと表示されません。
以下のコマンドで確認ができます。PowerShell 4.0では実行パフォーマンスがあり遅い*1のですが、PowerShellv5では大幅に改善されています。*2
ModuleがGraniResourceのものが見えれば正常に読み込めています。
リソースを使ったコンフィグレーションの構文
リソースには、それぞれ対応するコンフィグレーションのキーがありコンフィグレーション記述があります。リソースを設置したら見てみたいですよね?
では、GraniResourceのcACLリソースを使って、フォルダの権限を設定してみましょう。
リソースの読み込み
Microsoft標準リソース以外を利用する時は、コンフィグレーション構文の中でImport-DSCResource -ModuleName [ModuleName]
と記述するとモジュールに含まれるリソースがそのコンフィグレーション構文中で利用できます。
続いて、コンフィグレーション構文に則って、リソース名を使ってあるべき状態を書いてみましょう。
まずリソース名を載せてみると、Ctrl * Space
でインテリセンスが出ます。
[]
でキーが囲まれていないものが、必須のキーです。[]
でかかれているのはオプショナルです。
例えば、D:\test
フォルダに、D:\test
ユーザーに対してD:\test
の権限を与えるならこうです。
自分自身へのPush実行を行います。詳細は10日目にやるので説明は省略で!
結果は? うまくかかっていますね。
このような感じでカスタムリソースもさくっと使えるの大事です。
まとめ
Chef同様に、DSCもリソースで拡張されます。PowerShellの既存の仕組みに載せているので今までの経験が行かせるのも学習コストが小さくて楽だと思います。
では明日はいよいよ実行ですね! Pushで試してみましょう。