LoginSignup
1
1

More than 5 years have passed since last update.

Puppet4.xではじめるサーバ設定自動化 9(サービス管理)

Last updated at Posted at 2016-06-03

Puppet4.xではじめるサーバ設定自動化の、サービス管理編です。
全体的な目次は、ここを参照してください。
主にCentOS 7を例に紹介しているので、その他OSについては読み替えてください。

サービスの状態定義

基本

実行するサービスの設定は、Puppetのマニフェストではserviceリソースを使います。
httpdの例です。

コマンドでいう、

  • service httpd start
  • chkconfig httpd on

です。

service01.pp
service { "httpd" :
  ensure => running,
  enable => true,
}

  • serviceの右側のタイトルに、サービス名を指定
  • ensure属性に、状態を指定(実行はrunning, 停止はstopped)
  • enable属性に、OS起動直後にサービス起動するかしないかを指定(起動するはtrue, 起動しないはfalse)

設定ファイル変更後にサービス再起動

設定ファイル変更後に、サービスを再起動することもできます。
notify属性, subscribe属性, ~> (notifying arrow)の3通りの書き方があるので、それぞれについて紹介します。どの例も、設定ファイルに変更があった場合、サービスが再起動されます。設定ファイルの変更がない場合、(すでに起動済みの場合)再起動されません。

notify属性

notify属性の場合、設定ファイル側の属性に再起動するサービスを指定します。

restart01.pp
package { "httpd" :
  ensure => installed,
}

file { "/etc/httpd/conf/http.conf" :
  ensure  => file,
  owner   => "root",
  group   => "root",
  mode    => "0644",
  content => template("apache/httpd.conf.erb"),
  notify  => Service["httpd"], #####
}

service { "httpd" :
  ensure => running,
  enable => true,
}

なお、別の設定ファイルでもhttpdサービスの再起動が必要な場合、その設定ファイルにも同じようにnotify属性を書きます。

file { "/etc/httpd/conf.d/userdir.conf" :
  ensure  => file,
  owner   => "root",
  group   => "root",
  mode    => "0644",
  content => template("apache/userdir.conf.erb"),
  notify  => Service["httpd"],
}

subscribe属性

subscribe属性の場合、サービス側の属性に関連する設定ファイルを指定します。

restart02.pp
package { "httpd" :
  ensure => installed,
}

file { "/etc/httpd/conf/httpd.conf" :
  ensure  => file,
  owner   => "root",
  group   => "root",
  mode    => "0644",
  content => template("apache/httpd.conf.erb"),
}

service { "httpd" :
  ensure    => running,
  enable    => true,
  subscribe => File["/etc/httpd/conf/httpd.conf"], #####
}

なお、別の設定ファイルもhttpdサービスの再起動が必要な場合、その設定ファイルもsubscribe属性に指定します。複数の場合、次のような配列で指定します。(指定するファイルの配布定義も別途必要です)

service { "httpd" :
  ensure    => running,
  enable    => true,
  subscribe => [File["/etc/httpd/conf/httpd.conf"], File["/etc/httpd/conf.d/userdir.conf"]],
}

~> (notifying arrow)

notifying arrowの場合、
設定ファイル ~> サービス
で指定します。

restart03.pp
package { "httpd" :
  ensure => installed,
}

file { "/etc/httpd/conf/httpd.conf" :
  ensure  => file,
  owner   => "root",
  group   => "root",
  mode    => "0644",
  content => template("apache/httpd.conf.erb"),
}

service { "httpd" :
  ensure    => running,
  enable    => true,
}

File["/etc/httpd/conf/httpd.conf"] ~> Service["httpd"] #####

なお、別の設定ファイルもhttpdサービスの再起動が必要な場合、その設定ファイルも~>で指定します。(指定するファイルの配布定義も別途必要です)

File["/etc/httpd/conf/httpd.conf"] ~> Service["httpd"]
File["/etc/httpd/conf.d/userdir.conf"] ~> Service["httpd"]

再起動された場合のログ

notify属性などで再起動された場合、次のようなログがでます。

Info: /Stage[main]/Main/File[/etc/httpd/conf/http.conf]: Scheduling refresh of Service[httpd]
Notice: /Stage[main]/Main/Service[httpd]: Triggered 'refresh' from 1 events

最初の行は、/etc/httpd/conf/http.confが変わったのであとでhttpdを再起動しますという感じのログです。2行目は、実際に再起動されているログです。

ちなみに、複数ファイルが変わった場合は、再起動は1回のみです。2つのイベント(2 events)をトリガーに再起動(refresh)したという感じのログがでます。

Info: /Stage[main]/Main/File[/etc/httpd/conf/httpd.conf]: Scheduling refresh of Service[httpd]
...中略...
Info: /Stage[main]/Main/File[/etc/httpd/conf.d/userdir.conf]: Scheduling refresh of Service[httpd]
Notice: /Stage[main]/Main/Service[httpd]: Triggered 'refresh' from 2 events

3つの使い分け

再起動するのは同じなので、好みの書き方を選びましょう。
個人的には、次のようなイメージです。

  • notify : このファイルを変えると、あのサービスが再起動する
  • subcribe : このサービスは、あのファイルを変えると再起動する
  • ~> : 再起動の関連性をどこかにまとめて書きたい

ただ、混在して書くとわかりにくくなってしまうので、最初に1つ決めたらそれを全体で使うようにした方がよいです。

Puppetでサービス再起動?

設定ファイルの変更があったときにサービス再起動も自動化できますが、Puppetではサービス再起動しない方が良いこともあります。
例えば、設定ファイル変更とサービス再起動の時間帯を意図的に分けたい場合や、サービス間の起動停止に順序性がある場合などです。特に、サービス同士に順序性(同じサーバ内や別サーバ間)がある場合、Puppetで再起動は難しいのでnotifyなどで再起動定義せずに、SSH経由で再起動です。

SSH経由でコマンドを並列実行するツールは、以前まとめた投稿をご参考に。

参考

serviceリソースの詳しい情報は、下記参照です。

再起動の依存関係の書き方は、下記参照です。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1