Skip to content

Instantly share code, notes, and snippets.

@imksoo
Created April 5, 2017 07:24
Show Gist options
  • Save imksoo/40e87c185d1effd8612b898eca453d18 to your computer and use it in GitHub Desktop.
Save imksoo/40e87c185d1effd8612b898eca453d18 to your computer and use it in GitHub Desktop.
Itamaeならではの設定ファイルの修正方法

Itamaeならではの設定ファイルの修正方法

  • 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment