Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
24
Help us understand the problem. What is going on with this article?
@tt_yamaguchi

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

More than 5 years have passed since last update.

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
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
monstar-lab
自社サービス「モンスター・チャンネル」や「セカイラボ」をはじめ、日本を代表する企業のWeb・アプリの受託企画/開発を行っています。

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
24
Help us understand the problem. What is going on with this article?