Ruby/Chef include_recipe 并知道父文件中的变量?

JRE*_*EAM 0 ruby chef chef-solo

我正在尝试在包含的文件中使用局部变量。我收到未定义的错误。我不确定有什么方法可以做到这一点,是吗?我有一个配方文件夹:

recipes/
    development.rb
    testing.rb
    config.rb
Run Code Online (Sandbox Code Playgroud)

发展.rb

username = "vagrant"
static = []
django = ["project1", "project2"]

include_recipe "server::config"   # <-- Trying to use static and django in there.
Run Code Online (Sandbox Code Playgroud)

配置文件

static.each do |vhost|  #  <-- How do I get the "static" var in here?
    ...
end

django.each do |vhost|  #  <-- How do I get the "django" var in here?
    ...
end
Run Code Online (Sandbox Code Playgroud)

Art*_*son 5

您不能在配方之间共享变量,但有两种方法可以在配方之间共享数据。

  1. 首选路线是外部化staticdjango作为attributes/default.rb. 这意味着它们将在node对象上可用并且可以从每个配方访问。

属性/default.rb

default["server"]["static"] = []
default["server"]["django] = ["project1", "project2"]
Run Code Online (Sandbox Code Playgroud)

食谱/config.rb

node["server"]["static"].each do |vhost|
  ...
end

node["server"]["django"].each do |vhost|
  ...
end
Run Code Online (Sandbox Code Playgroud)
  1. 使用Chef 库创建一个返回这些数组的通用方法。

我的建议是绝对坚持选项一,这是最常见的方法。希望有帮助!