IT井戸端会議

IT井戸端会議

インフラ、ネットワーク、アプリケーション開発、IT界隈の話等々を東京都千代田区界隈から発信します。

chef-soloのレシピ例

ファイルのダウンロード(command)

execute "down" do
  command <<-EOC
    wget "https://dl.bintray.com/mitchellh/vagrant/vagrant_1.7.2_x86_64.rpm"
  EOC
end

ファイルのダウンロード(remote_file)

remote_file "/root/chef-repo/cookbooks/download/files/vagrant_1.7.2_x86_64.rpm" do
  source "https://dl.bintray.com/mitchellh/vagrant/vagrant_1.7.2_x86_64.rpm"
end$ mkdir /etc/chef

rpmパッケージのインストール

rpm_package "vagrant_install" do
  source "/root/chef-repo/cookbooks/rpm_test/files/default/vagrant_1.7.2_x86_64.rpm"
  action :install
end

 

 

ファイル編集

 

ファイルにworkの文字列が存在しない場合のみ追記

bash "add text" do
  not_if 'grep "work" /root/testfile'
  code <<-EOC
  echo "work" >> /root/testfile
  EOC
end

ファイルにokの文字列が存在した場合に置換①

execute 'replace_sed' do
  command "sed -i -e 's/ok/NG/g' /root/testfile"
  action :run
end

ファイルにokの文字列が存在した場合に置換②

file "/root/testfile" do
  f = Chef::Util::FileEdit.new(path)
  f.search_file_replace('ok','NG')
  f.write_file
end

 

ディレクトリを作成

directory "/root/test_dir" do
  owner "root"
  group "root"
  mode 0755
 action :create
end

ファイルを作成(作成時に"Chef file test"を記載)

file "/root/testfile" do
        content "Chef file test"
        owner "root"
        group "root"
        mode 00755
        action :create
end

サービス起動

service "ntpd" do
  action :start
end

ntpdをchkconfigコマンドを使用して設定がoffの場合のみonに設定

bash "start ntpd" do
  only_if "chkconfig --list ntpd | grep 3:off"
  code <<-EOC
  chkconfig ntpd on
 EOC
end

cookbookのtemplate/test/testfile.erbを/root/testfileに配布

template "/root/testfile" do
  source 'test/testfile.erb'
  mode '0700'
  owner 'root'
end