如何使用 Chef 和包含指令的 .rb 檔案設定 vagrant VM

如何使用 Chef 和包含指令的 .rb 檔案設定 vagrant VM

我可能整個概念都是錯的:我有一個運行 Ubuntu 12.04 的 Vagrant VM,在它上面我想安裝一些套件和設定檔。我將它們設定在 Chef 中,在路徑 Cookbooks/my_project/recipes 中,我有一個包含所有說明的 vagrant-dev.rb 檔案。現在我在 Vagrantfile 中的 Vagrant 配置一定是這裡的問題:

config.vm.provision :chef_solo do |chef|
    chef.cookbooks_path = "cookbooks/my_project/recipes"
    chef.add_recipe "vagrant-dev.rb"
end

當我加載虛擬機器時我得到

FATAL: Chef::Exceptions::CookbookNotFound: Cookbook vagrant-dev.rb not found.

我嘗試最後沒有 .rb 。我想這是完全不同的事情,我沒有以正確的方式使用它。但搜尋後我找不到任何解釋如何正確執行此操作的內容。

答案1

使用廚師時,所有食譜必須出現在食譜中。看起來你可能已經在食譜中找到了它,只是稱它為錯誤的,但我會涵蓋所有內容,所以你可以仔細檢查。

食譜其實只是食譜 rb 檔案、一些元資料以及可選的一些其他檔案(如範本或資料包)的集合。因此,您不能直接包含 .rb 文件,您必須引用其說明書,然後引用文件名稱(不含 .rb)才能運行它。

一個簡單的食譜的結構應該是這樣的:

SomeCookbook
    readme.md # needed for the long_description in metadata to work
    metadata.rb # contains the actual information for the cookbook
    recipes # Holds all the cookbook's recipies
        default.rb # This is the default recipe, run if one isn't specified
        otherRecipe.rb
    templates # Templates that can be called by the cookbook
        default
            some-erb-style-template.erb

主目錄的名稱並不重要,模板目錄是可選的。

metadata.rb

name             "SomeCookbook"
maintainer       "Me"
maintainer_email "[email protected]"
license          "None"
description      "Does something cool"
long_description IO.read(File.join(File.dirname(__FILE__), 'readme.md'))
version          "0.0.1"

supports "centos"

遵循上述結構,並確保將您的食譜貼到食譜資料夾中。
然後將其添加到您的 vagrant 文件中:

chef.add_recipe "SomeCookbook::vagrant-dev"

希望以上內容能澄清一些事情。

相關內容