Chef で対話的な問い合わせが必要なパッケージをインストールする

Debian でパッケージをインストールすると対話的な問い合わせがある場合があります。
例えば、mysql-community-server*1 をインストールすると MySQL root ユーザのパスワードを聞かれたりします。
こうしたパッケージを Chef でインストールする場合は、応答する内容を記述したファイルを用意し、response_file を設定することで対話的な問い合わせをパスすることができます。

recipe

package "mysql-community-server" do
  action :install
  response_file "mysql.preseed"
end

response_file

response_file に指定したファイルは、/Path-to-Your-Cookbooks/files/default 配下に作成します。
記述する内容は、debconf-show コマンドで確認します。

$ sudo debconf-show mysql-community-server
* mysql-community-server/root-pass: 
* mysql-community-server/re-root-pass: 
  mysql-community-server/remove-data-dir:
  mysql-community-server/root-pass-mismatch:
* mysql-community-server/data-dir:

今回は、以下の感じのファイルを準備しました。
「12345678」が root ユーザのパスワードになります。

mysql-community-server mysql-community-server/root-pass password 12345678
mysql-community-server mysql-community-server/re-root-pass password 12345678

response_file を使用することで、対話的な問い合わせがあるパッケージのインストールも自動化できます。
package — Chef Docs