如何使用Vagrantfile中的Chef数据包?

时间:2014-01-20 16:26:45

标签: ruby json chef vagrant

我正在尝试使用Fnichol's Chef User配方与Vagrant在我运行vagrant up时自动创建特定的用户帐户。因为我几乎为我工作的每个项目设置了这个用户,所以我想将用户数据存储在我正在从JSON文件加载的data bag中,我可以在多个项目中重复使用。

这就是我Vagrantfile目前的样子:http://pastebin.com/b0riZZCz

失败并显示错误:

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

我创建了一个名为“users”的data bag,在该数据包中,我创建了一个名为“ben”的data bag item来自以下JSON:

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

我正在尝试按照http://fnichol.github.io/chef-user/上的使用说明进行操作:

  1. 导入Chef Users配方
  2. 告诉Chef Users配方从数据包ben
  3. 创建用户users

    在我看来,Vagrantfile中的Chef配置语法与大多数可用文档中提供的语法有很大不同。例如:

      

    “只需​​在recipe[user]run_list中添加user_account即可   资源将可用。“

    我对run_list的定义以及recipe[user]语法感到困惑。从Vagrant documentation开始,我的run_list似乎就是这个街区中的所有内容:

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

    但是,我在chef.run_list内定义了also found referenceschef.run_list,但我找不到任何专门提及run_list的文档。

    问题1

    config.vm.provision :chef_solo do |chef|只是chef.run_list块中的代码,还是其他内容?

    问题2

    Chef User的文档在哪里?我不是在Chef网站上查找文档。语法似乎与我完全不同。

    我已经搞乱了几个小时。我尝试了一堆不起作用的东西。我能够导入user食谱,但我无法弄清楚如何告诉Chef对data bag item“ben”运行user食谱。

    问题3

    如何让Chef使用data bag item“ben”运行{{1}}食谱?或者我完全错了吗?

1 个答案:

答案 0 :(得分:4)

回答1

不,run_list实际上只是一个字符串数组,表示应该在Vagrant Machine上运行的配方/角色。 add_recipe将新配方添加到此列表中。像那样:

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

回答2

您正在编辑Vagrantfile,因此文档位于Vagrant site

回答3

你需要告诉Vagrant你的食谱,数据包和角色在哪里。

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