- Itamaeの
file
resourceではblock
(Ruby文) を書ける block
の中では対象ファイルの内容を受け取って書き換えることが出来る
service 'sshd'
file '/etc/ssh/sshd_config' do
action :edit
block do |content|
# "=~"は正規表現にマッチする場合にtrueを返す
if content =~ /^PasswordAuthentication/i then
# gsub!は正規表現に基づく置換
content.gsub!(/^PasswordAuthentication.*$/i, 'PasswordAuthentication yes')
# マッチしない場合 (演算子だと !~ でも可能)
else
content << "\nPasswordAuthentication yes\n"
end
end
notifies :reload, 'service[sshd]'
end