Puppet で PowerShell スクリプトを実行する

投稿者: Dan Franciscus 投稿日: 2018年10月22

スクリプトが非常に複雑で構成管理が必要になるような場合があります。スクリプトを書き直してマニフェストを作成するのは大変時間がかかる作業です。このブログでは、Puppet から PowerShell スクリプトを実行する方法を説明します。

Windows のための Puppet サポートは増えつつあります。Puppet 自体も、Puppet マニフェストの中で Desired State Configurationリソースを使えるようにすることに加えて、ネイティブな Windows モジュールを作成しています。そして、実際、Puppet を使って PowerShell スクリプトを実行することもできます。これはちょっとした難点もあり、Windows サーバーを管理する方法としては理想的とは言えないことはお断りしないといけませんが、Puppet ではかなり支持されています。

Puppet の exec リソース

Puppet の "exec" リソースを使って、ノード上でコマンドとスクリプトを実行することができます。これは Linux の bash で使用できますが、PowerShell provider を使用すると、Window 上でも Linux ノードでも PowerShell を実行できます。

PowerShell で exec リソースを作成する際に使用する重要なパラメータがいくつかあります。それらは、commandonlyifreturnsunless の4つです。command パラメータは、ノードで実行するコマンドまたはスクリプトを指定します。onlyifunless は、Puppet 内でパラメータのアウトプットに応じてコマンドが実行される条件付き演算子のように機能します。以下で、これらがどのように動作するかを説明します。

ノード上でサンプルの Puppet マニフェストを実行

下の例は、ホスト名がスクリプトで指定された名前と異なる場合にのみ、ホスト名を設定する PowerShell スクリプトを Windows ホストで実行するもので、"rename_hostname" という名前の Puppet サンプル・モジュールです。

class rename_hostname {

exec { 'win-hostname':

  command   => file('rename_hostname/rename-hostname.ps1'),

  onlyif    => file('rename_hostname/hostname-exists.ps1'),

  provider  => powershell,

  logoutput => true,

  }

}

2つの PowerShell スクリプトは、Puppet モジュールのサブディレクトリ "files" の下の "rename_hostname" に保存されています。”files” は、Puppet が、file() を使用してマニフェストに記述されているスクリプトを探すときのデフォルトです。

この設定をノードに適用すると、最初に "hostname-exists.ps1" が実行され、このスクリプトが終了コード0で停止すると、"rename-hostname.ps1" が実行されます。スクリプトが終了コード0で終了しない場合、PowerShell スクリプトの "rename-hostname.ps1" は実行されません。Puppet のパラメータ、onlyif は、このように動作します。

関連ブログ: Setting Up Your First Puppet Master Server

上で使っている2つの PowerShell スクリプトの内容は次の通りです。

  1. hostname-exists.ps1
if ($env:computername -ne ‘test-1’) {

   write-output 'Change hostname'

   exit 0

}

else {

   write-output 'hostname is correct'

   exit 1

}
  1. rename-hostname.ps1
Rename-Computer -NewName puppetagent-win -Force -Confirm:$false

ノード "test-1" においてホスト名が "test-1" 以外の名前に変更してあったので、Puppet 設定を適用するとホスト名が変更されます。

C:\Users\Administrator>puppet agent --test --certname test-1

Info: Retrieving pluginfacts                                                                        

Info: Retrieving plugin                                                                             

Info: Loading facts                                                                                  

Info: Caching catalog for test-1

Info: Applying configuration version '1530211488'                                                   

Notice: /Stage[main]/Rename_hostname/Exec[win-hostname]/returns: WARNING: The ch                     

anges will take effect after you restart the computer what.                                         

Notice: /Stage[main]/Rename_hostname/Exec[win-hostname]/returns: executed succes                    

sfully                                                                                              

Notice: Finished catalog run in 2.87 seconds  

この設定をもう一度適用すると、ホスト名はすでに "test-1" に設定されているので、変更は実施されません。

C:\Users\Administrator>puppet agent --test –certname=test-1

Info: Retrieving pluginfacts                                                                        

Info: Retrieving plugin                                                                              

Info: Loading facts                                                                                 

Info: Caching catalog for test-1

Info: Applying configuration version '1530213484'                                                   

Notice: Finished catalog run in 2.62 seconds

まとめ

Puppet を使用することの利点は、スクリプトの代わりに簡単に設定を書けることですが、特に Puppet に慣れておらず、PowerShell の経験が長いユーザーの場合など、PowerShell を使った方が速くて簡単なこともあります。Puppet は Windows の機能追加のために尽力しましたが、Windows のユーザーが Puppet マニフェストに PowerShell スクリプトを書けることで利便性が向上しました。


Dan Franciscus
Dan Franciscus is a systems engineer and VMware Certified Professional (VCP) specializing in VMware, PowerShell, and other Microsoft-based technologies. You can reach Dan at his blog (http://www.winsysblog.com/) or Twitter at @dan_franciscus.
著者が作成したブログ

関連タグ

Prefooter Dots
Subscribe Icon

Latest Stories in Your Inbox

Subscribe to get all the news, info and tutorials you need to build better business apps and sites

The specified form no longer exists or is currently unpublished.