Puppet4.xではじめるサーバ設定自動化の、実行編です。
全体的な目次は、ここを参照してください。
実行コマンド
スタンドアローンで実行する方法を紹介します。
実行で反映させる定義(マニフェスト)を、用意します。カレントディレクトリに下記の、sample.ppをエディタで編集してつくります。今回は/tmpディレクトリに、hello_puppet.txtを作るマニフェストです。
file { "/tmp/hello_puppet.txt":
content => "Hello Puppet!\n",
}
実行する
puppetのマニフェストをスタンドアローンで実行する場合、applyを使います。
/opt/puppetlabs/bin/puppet apply <反映するマニフェストファイル>
先ほどのマニフェストで実行してみます。※もし、すでに/tmp/hello_puppet.txtがある場合は、事前に移動させておいてください。
# /opt/puppetlabs/bin/puppet apply sample.pp
Notice: Compiled catalog for 8fd260a416a1 in environment production in 0.03 seconds
Notice: /Stage[main]/Main/File[/tmp/hello_puppet.txt]/ensure: defined content as '{md5}65a0b93ae3ce3b139b50be784e4d286e'
Notice: Applied catalog in 0.03 seconds
# cat /tmp/hello_puppet.txt
Hello Puppet!
よく使うオプション
applyでよく使うオプションの紹介です。
- --noop: Puppetをno-opモード(dry-runモード)で実行するオプション。実際に変更せずに、Puppetで何が変わるかを、事前に確認するときに便利です。
- --test: 下記のオプションがつきます。-tでも同じです。
- verbose: より多くの情報を表示
- detailed-exitcodes: リータンコードが詳細になります。
- 0: puppet実行成功。サーバへの変更なし、マニフェストのエラーなし。
- 1: puppet実行失敗。
- 2: puppet実行成功。サーバへの変更あり。
- 4: puppet実行成功。サーバへの変更で失敗。
- 6: puppet実行成功。サーバへの変更あり。一部変更に失敗。
- show_diff: テストファイル内の変更diffを表示
- --modulepath: moduleを使う場合のパス指定。Puppetでは、共通的な部品を、クラスで定義できます。そのクラスを使う場合にパスを指定します。
noop
noopオプションの例です。
先ほどのマニフェストで実行してみます。※もし、すでに/tmp/hello_puppet.txtがある場合は、事前に移動させておいてください。
実際には変更されていないので、ログに(noop)がでます。
# /opt/puppetlabs/bin/puppet apply sample.pp --noop
Notice: Compiled catalog for 8fd260a416a1 in environment production in 0.03 seconds
Notice: /Stage[main]/Main/File[/tmp/hello_puppet.txt]/ensure: current_value absent, should be file (noop)
Notice: Class[Main]: Would have triggered 'refresh' from 1 events
Notice: Stage[main]: Would have triggered 'refresh' from 1 events
Notice: Applied catalog in 0.02 seconds
# cat /tmp/hello_puppet.txt
cat: /tmp/hello_puppet.txt: No such file or directory
test
testオプションの例です。
先ほどのマニフェストで実行してみます。※もし、すでに/tmp/hello_puppet.txtがある場合は、事前に移動させておいてください。
※今回は、差分でるか確認するためなので、一緒にnoopオプションもつけていますが、testオプションだけでも実行できます。
テキストファイルがない場合は、テキストの中身の差分は表示されません。
# /opt/puppetlabs/bin/puppet apply sample.pp -t --noop
Notice: Compiled catalog for 8fd260a416a1 in environment production in 0.03 seconds
Info: Applying configuration version '1460769891'
Notice: /Stage[main]/Main/File[/tmp/hello_puppet.txt]/ensure: current_value absent, should be file (noop)
Notice: Class[Main]: Would have triggered 'refresh' from 1 events
Notice: Stage[main]: Would have triggered 'refresh' from 1 events
Notice: Applied catalog in 0.02 seconds
すでにファイルがあって、内容に差分がある場合は、diff結果が表示されます。
# touch /tmp/hello_puppet.txt
# /opt/puppetlabs/bin/puppet apply sample.pp -t --noop
Notice: Compiled catalog for 8fd260a416a1 in environment production in 0.03 seconds
Info: Applying configuration version '1460770131'
Notice: /Stage[main]/Main/File[/tmp/hello_puppet.txt]/content:
--- /tmp/hello_puppet.txt 2016-04-16 01:28:46.997698218 +0000
+++ /tmp/puppet-file20160416-45-1156wf4 2016-04-16 01:28:51.177698218 +0000
@@ -0,0 +1 @@
+Hello Puppet!
Notice: /Stage[main]/Main/File[/tmp/hello_puppet.txt]/content: current_value {md5}d41d8cd98f00b204e9800998ecf8427e, should be {md5}65a0b93ae3ce3b139b50be784e4d286e (noop)
Notice: Class[Main]: Would have triggered 'refresh' from 1 events
Notice: Stage[main]: Would have triggered 'refresh' from 1 events
Notice: Applied catalog in 0.03 seconds
参考
puppet applyのより詳しい情報は、下記参照。