LoginSignup
3
3

More than 5 years have passed since last update.

chefでファイルコピーするときにruby_blockは使わないでfileを使う(ファイルに更新がなければサービスの再起動しない)

Posted at

何が気になったか

  • chefの中でファイルコピーするのにruby_blockを使っている処理を見た
  • block内で notifies :restart, 'service[hogehoge]' が書かれてて、ruby_blockなもんで、100% hogehoge が restart されてた
  • で、ファイルをコピーする方法が以下のURLに書かれていたんだけど、なんか見慣れない記述…
  • ファイル更新されてない場合は notifies :restart, 'service[hogehoge]' は実行されないか確認したくなった

filecopy
file '/root/1.txt' do
  content IO.read('/tmp/1.txt')
  action :create
end

参考URL

ChefでCookbookを作成するときのちょっとしたコツ 9選 - インフラエンジニアway - Powered by HEARTBEATS
http://heartbeats.jp/hbblog/2013/01/chef-cookbook-tips.html

file — Chef Docs
https://docs.chef.io/resource_file.html

テスト環境

使ったもの

Chef DK

test-kitchen, berkshelf あたりでさくっと試す。
Chef DK のお陰で cookbook や chef repo のテンプレを作成しやすくなった。

$ chef -v
Chef Development Kit Version: 0.6.0
chef-client version: 12.3.0
berks version: 3.2.4
kitchen version: 1.4.0
$

cookbook

apache2 を使う

Cookbooks - Chef Supermarket
https://supermarket.chef.io/cookbooks?utf8=%E2%9C%93&q=apache2&platforms%5B%5D=

準備

cookbook テンプレを作る

$ chef generate cookbook chef-copy-test
$ cd chef-copy-test

apache2 を使う設定

Berksfile を編集

最後の1行を追加

source 'https://supermarket.chef.io'

metadata
cookbook 'apache2', '~> 3.1.0'

metadata.rb を編集

これも最後の1行を追加

name 'chef-copy-test'
maintainer 'The Authors'
maintainer_email 'you@example.com'
license 'all_rights'
description 'Installs/Configures chef-copy-test'
long_description 'Installs/Configures chef-copy-test'
version '0.1.0'

depends 'apache2', '~> 3.1.0'

recipe を作る

/etc/grub.conf をコピーして
/root/grub.conf.0
/root/grub.conf.1
/root/grub.conf.2
/root/grub.conf.3
/root/grub.conf.4
/root/grub.conf.5
/root/grub.conf.6
/root/grub.conf.7
/root/grub.conf.8
/root/grub.conf.9

を作る recipe を書く。

#
# Cookbook Name:: chef-copy-test
# Recipe:: default
#
# Copyright (c) 2015 The Authors, All Rights Reserved.
include_recipe 'apache2'

digits = 0..9

digits.each do |digit|
    file "/root/grub.conf.#{digit}" do
        content IO.read('/etc/grub.conf')
    action :create
    notifies :restart, 'service[apache2]', :delayed
  end
end

1回目の適用

restart service service[apache2] が最後に実行される。

1回目
$ chef exec kitchen converge
(省略)

           - update content in file /root/grub.conf.9 from none to d3c095
           --- /root/grub.conf.9    2015-09-01 14:26:07.355294634 +0000
           +++ /root/.grub.conf.920150901-2779-yrtqwr   2015-09-01 14:26:07.353294634 +0000
           @@ -1 +1,18 @@
           +# grub.conf generated by anaconda
           +#
           +# Note that you do not have to rerun grub after making changes to this file
           +# NOTICE:  You have a /boot partition.  This means that
           +#          all kernel and initrd paths are relative to /boot/, eg.
           +#          root (hd0,0)
           +#          kernel /vmlinuz-version ro root=/dev/mapper/VolGroup-lv_root
           +#          initrd /initrd-[generic-]version.img
           +#boot=/dev/sda
           +default=0
           +timeout=5
           +splashimage=(hd0,0)/grub/splash.xpm.gz
           +hiddenmenu
           +title CentOS (2.6.32-431.el6.x86_64)
           +    root (hd0,0)
           +    kernel /vmlinuz-2.6.32-431.el6.x86_64 ro root=/dev/mapper/VolGroup-lv_root rd_NO_LUKS LANG=en_US.UTF-8 rd_NO_MD rd_LVM_LV=VolGroup/lv_swap SYSFONT=latarcyrheb-sun16 crashkernel=auto rd_LVM_LV=VolGroup/lv_root  KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet

           - restore selinux security context
       Recipe: apache2::default
       * service[apache2] action restart
           - restart service service[apache2]

       Running handlers:
       Running handlers complete
       Chef Client finished, 11/74 resources updated in 7.041462979 seconds
       Finished converging <default-centos-65> (0m10.90s).
-----> Kitchen is finished. (0m12.61s)
$

2回目の適用

apache は restartされない

$ chef exec kitchen converge

(省略)

       Recipe: chef-copy-test::default
        (up to date)
         * file[/root/grub.conf.1] action create (up to date)
        (up to date)
        (up to date)
        (up to date)
        (up to date)
        (up to date)
         * file[/root/grub.conf.7] action create (up to date)
        (up to date)
        (up to date)

       Running handlers:
       Running handlers complete
       Chef Client finished, 0/73 resources updated in 4.066823513 seconds
       Finished converging <default-centos-65> (0m7.24s).
-----> Kitchen is finished. (0m8.29s)
$

満足。

github

今回作成したcookbookはこちらに置きました

3
3
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
3
3