13
Help us understand the problem. What are the problem?

More than 5 years have passed since last update.

posted at

updated at

Chefで文字列置換をする

chefで文字列置換しようと思ったらちょっとはまったのでメモ
Chef::Util::FileEditを使う

file '/home/user/hogehoge' do
  _file = Chef::Util::FileEdit.new(path)
  #マッチした次の行に挿入
  _file.insert_line_after_match(/regex/, 'newline')
  #マッチしなかったら文末に挿入
  _file.insert_line_if_no_match(/regex/, 'newline')
  #マッチした文字列/行を削除
  _file.search_file_delete(/regex/)
  _file.search_file_delete_line(/regex/)
  #マッチした文字列/行を置換
  _file.search_file_replace(/regex/, 'replace')
  _file.search_file_replace_line(/regex/, 'replace')

  #old_fileを作ったうえで置換
  _file.write_file
end

馬鹿っぽいけど正規表現は//でくくんなきゃだめだよね。。。
' 'でくくっててはまった

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
13
Help us understand the problem. What are the problem?