大厨:来自变量的模板 'name'?

Chef: template 'name' from variable?

我在 attributes/default.rb 中有一个属性:

default["host_name"] = "domain.com"

并希望使用来自该属性的主机名创建 NGINX 的配置:

...
template "/etc/nginx/conf.d/#{@host_name}.conf" do
  source 'domain.conf.erb'
  owner 'root'
  group 'root'
  mode '0644'
end
...

但是在构建过程中 - Chef 看不到 host_name:

 ...
 [14:18:28][Step 1/1] Recipe: nginx_proxy::default
 [14:18:28][Step 1/1]   * template[/etc/nginx/conf.d/.conf] action create
 [14:18:28][Step 1/1]     - create new file /etc/nginx/conf.d/.conf
 [14:18:28][Step 1/1]     - update content in file /etc/nginx/conf.d/.conf rom none to cc9a26
 ...

这是怎么回事?完全可以实现吗?

Chef 文档对 template 的 "dynamic" name 只字未提。

您需要使用 node 对象来 access its attributes。您尝试使用 @host_name 访问的是局部变量。

以下应该有效:

template "/etc/nginx/conf.d/#{node['host_name']}.conf" do
  source 'domain.conf.erb'
  owner 'root'
  group 'root'
  mode '0644'
end