24
24

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Chefでopscode-cookbooksのntpレシピを使う

Last updated at Posted at 2013-05-29

opscodeのpublic cookbooksを使ってntpによる時刻合わせをするサンプルです。

このサンプルでは、chef-soloを使ってローカルホストに対してレシピを実行します。
レシピ管理にはBerkshelfを使い、serverspecでテストします。

使用するレシピ

Opscode Cookbook ntp
https://github.com/opscode-cookbooks/ntp

レシピのダウンロード

Berksfile
site :opscode
cookbook 'ntp'
bundle exec berks install --path vendor/cookbooks

レシピを読む

vendor/cookbooks/ntp/recipes/default.rb
# ntpパッケージをインストールする
node['ntp']['packages'].each do |ntppkg|
  package ntppkg
end

# ntpが使うディレクトリの所有者と権限を設定する
[ node['ntp']['varlibdir'],
  node['ntp']['statsdir'] ].each do |ntpdir|
  directory ntpdir do
    owner node['ntp']['var_owner']
    group node['ntp']['var_group']
    mode 0755
  end
end

# ntpサービスを開始し、自動起動設定する
service node['ntp']['service'] do
  supports :status => true, :restart => true
  action [ :enable, :start ]
end

# files/default/ntp.leapsecondsファイルを
# node['ntp']['leapfile']のパスに配置する
cookbook_file node['ntp']['leapfile'] do
  owner node['ntp']['conf_owner']
  group node['ntp']['conf_group']
  mode 0644
end

# 設定ファイルを作成し、サービスを再起動する
template "/etc/ntp.conf" do
  source "ntp.conf.erb"
  owner node['ntp']['conf_owner']
  group node['ntp']['conf_group']
  mode "0644"
  notifies :restart, resources(:service => node['ntp']['service'])
end

レシピの実行

default['ntp']['servers']アトリビュートに時刻同期先のサーバを設定します。

nodes/localhost.json
{
  "ntp": {
    "servers": ["ntp.nict.jp", "ntp.jst.mfeed.ad.jp"]
  },
  "run_list": [
    "recipe[ntp]"
  ]
}

sudo bundle exec chef-solo -c solo.rb -j nodes/localhost.json

serverspecでテスト

spec/localhost/ntp_spec.rb
require 'spec_helper'

describe package('ntp') do
  it { should be_installed }
end

describe service('ntpd') do
  it { should be_running }
  it { should be_enabled }
end

describe file('/etc/ntp.conf') do
  it { should be_file }
  it { should contain '^server ntp.nict.jp' }
  it { should contain '^server ntp.jst.mfeed.ad.jp' }
end

# アスタリスク "*" が左端に表示され参照中であること
describe command('ntpq -pn') do
  it { should return_stdout /^\*\d/}
end
bundle exec rake spec 

Package "ntp"
  should be installed

Service "ntpd"
  should be running
  should be enabled

File "/etc/ntp.conf"
  should be file
  should contain "^server ntp.nict.jp"
  should contain "^server ntp.jst.mfeed.ad.jp"

Command "ntpq -pn"
  should return stdout /^\*\d/
24
24
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
24
24

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?