Ben Harold
Ben Harold

Reputation: 6432

How do I use Chef data bags from my Vagrantfile?

I am attempting to use Fnichol's Chef User recipe with Vagrant to automatically create a specific user account when I run vagrant up. Because I setup this user for nearly every project that I work on, I'd like to store the user data in a data bag that I am loading from a JSON file that I can re-use on multiple projects.

This is what my Vagrantfile currently looks like: http://pastebin.com/b0riZZCz

It fails with the error:

[2014-01-20T16:09:39+00:00] ERROR: could not find recipe ben for cookbook user

I have created a data bag called "users" and inside that data bag I've created a data bag item named "ben" from the following JSON:

{
  "id": "ben",
  "comment": "Ben Harold",
  "groups": ["admin", "www-data"],
  "ssh_keys": ["...longtextstring..."],
  "ssh_keygen": false
}

I'm attempting to follow the usage instructions at http://fnichol.github.io/chef-user/ to:

  1. Import the Chef Users recipe
  2. Tell the Chef Users recipe to create the user ben from the data bag users

It appears to me that the Chef provisioning syntax from within a Vagrantfile is considerably different than the syntax that is presented in most of the available documentation. For example:

"Simply include recipe[user] in your run_list and the user_account resource will be available."

I'm confused as to the definition of run_list, as well as the recipe[user] syntax. From the Vagrant documentation, it seems that my run_list is everything in this block:

config.vm.provision :chef_solo do |chef|
  chef.add_recipe "apt"
  ...etc...
end

However, I've also found references to chef.run_list being defined within that block, although I have not been able to find any documentation specifically referring to chef.run_list.

Question 1

Is the run_list simply the code within the config.vm.provision :chef_solo do |chef| block, or is it something else?

Question 2

Where is the documentation for chef.run_list? I'm not looking for the documentation on the Chef site. The syntax seems completely different to me.

I've been messing with this for several hours. I've tried a bunch of stuff that does not work. I am able to import the Chef User recipe, but I haven't been able to figure out how to tell Chef to run the user recipe against the data bag item "ben".

Question 3

How do I get Chef to run the user recipe with the data bag item "ben"? Or am I doing it completely wrong?

Upvotes: 3

Views: 2561

Answers (1)

Draco Ater
Draco Ater

Reputation: 21206

Answer 1

No, run_list is actually just an array of strings, that represent recipes/roles that should be run on Vagrant Machine. And add_recipe adds new recipes into this list. Like that:

config.vm.provision :chef_solo do |chef|
  [...]
  chef.add_recipe 'cookbook::recipe' #now run_list has it in
  chef.add_role 'myrole' #now run_list has a role too
  #adding more recipes from ENV variable, just as example
  chef.run_list += ENV['CHEF_RUN_LIST'].split ',' if ENV.has_key? 'CHEF_RUN_LIST' 
  [...]
end

Answer 2

You are editing Vagrantfile, so documentation is on Vagrant site

Answer 3

You need to tell Vagrant where are your cookbooks, data bags and roles.

config.vm.provision :chef_solo do |chef|
  [...]
  chef.cookbooks_path = 'cookbooks' #paths are relative to Vagrantfile location
  chef.roles_path = 'roles'
  chef.data_bags_path = 'data_bags'
  [...]
end

Upvotes: 4

Related Questions