Provide debconf variable from data bag? (Newbie)

I’m just starting to learn my way around chef by designing recipes and cookbooks to install a distributed application.

One of the first things I need to do is install mariadb on one of my servers. Because many recipes will need the database root password, I’ve put the root password in a data bag. (I think that’s an appropriate choice, no?)

To install the database server, a recipe will have a section similar to:

package 'database-server' do
   package_name 'mariadb-server'
   action [:install]

My question: how can I take the root password out of the databag and supply it to dpkg so that the database server installation succeeds and the root password is the one I gave?

The debconf variable names needed (from debconf-get-selections) are, I believe, mysql-server/root_password and mysql-server/root_password_again.

Basically you just need to create a debian pressed file and run debconf-set-selections FILE_PATH. Be sure to set the DEBIAN_FRONTEND env var to “noninteractive” so it doesn’t pop up a modal dialog. You could do that manually with a template file and execute resource, which might be the easiest way.

Chef also has built-in support for this in the package resource, but it can be a bit tricky to use. if you want to use it, create a template for the response file, then set the response_file property on the resource. Standard naming convention is $package_name.seed.erb. You set the variables to pass in with the response_file_variables property (it’s a Hash, like you would give to a normal template resource). Then the package resource should automatically find the template and evaluate it and run debconf-set-selections for you when installing.

Either way, you’ll probably want to run in debug mode while you’re getting this all working, using hard coded values at first to make your task simpler, then change the code to pull the values from a data bag which should be pretty straightforward once the other stuff works.

Thanks for the input, but I’m afraid I’m still having difficulties. It appears that running debconf-set-selections in my recipe isn’t doing anything. I have:

bash 'set_apt_noninteractive' do
  command 'export DEBIAN_FRONTEND="noninteractive"'

cookbook_file 'mariadb_preseed_answers' do
  source 'mariadb-preseed'
  mode '777'
  path '/home/mariadb_preseed.txt'

bash 'load_mariadb_preseed' do
  command "debconf-set-selections /home/mariadb_preseed.txt"

bash 'list_debconf_variables' do
  command "debconf-get-selections > /home/debconfselections.txt"

I’m finding that, though the client reports that everything runs without error, the debconf settings aren’t getting set, and /home/debconfselections.txt isn’t getting created. However, /home/mariadb_preseed.txt is getting copied over, and if I run debconf-set-selections /home/mariadb_preseed.txt at the command line, the settings take effect.

I’m afraid I’m new enough to Chef that I don’t know how to troubleshoot this from here. If I do install mariadb-server from within the recipe, it doesn’t use the password specified in the preseed file. If I install mariadb-server from the command line after running debconf-set-selections, the DB server installs with the password I specified in the file.

It seems as if the shell command debconf-set-selections isn’t doing anything when run by the Chef client, but I suspect the true explanation is a bit more involved than that.

Any further trouleshooting suggestions from the community would be appreciated.

I found that changing my bash block to an execute block to run debconf-set-selections did the trick.

If anyone can give me the quick version of why one works and the other doesn’t, I’d appreciate the enlightenment.

Thanks, everyone!

Just wanted to point out that code like that won’t do anything for you. What it does is it starts a bash process, which then sets an environment variable for itself, and then exits. This won’t have any effect on any subsequent bash resources you run. You instead want to use the env parameter for execute/bash/other script resources, which will set the environment for the process when chef launches it.