【问题标题】:chef template variable @node seems to throw error厨师模板变量@node似乎抛出错误
【发布时间】:2013-07-16 00:55:20
【问题描述】:

我正在阅读 peepcode 的 Chef 教程,到目前为止一切顺利。由于某种原因,使用模板变量时会失败。该示例适用于 nginx。

在 nginx/attributes/nginx.rb 我有:

default[:nginx][:worker_processes] = 4

在我参考的nginx.conf.erb模板中:

worker_processes  <%= @node[:nginx][:worker_processes] %>;

以下是我在运行 chef-solo 时遇到的错误:

Template Context:
-----------------
on line #2
  1: user www-data;
  2: worker_processes  <%= @node[:nginx][:worker_processes] %>;
  3: 
  4: error_log  /var/log/nginx/error.log;
  5: pid        /var/run/nginx.pid;


[2013-07-14T19:46:36+02:00] ERROR: Running exception handlers
[2013-07-14T19:46:36+02:00] ERROR: Exception handlers complete
Chef Client failed. 0 resources updated
[2013-07-14T19:46:36+02:00] FATAL: Stacktrace dumped to /var/chef/cache/chef-stacktrace.out
[2013-07-14T19:46:36+02:00] FATAL: Chef::Mixin::Template::TemplateError: undefined method `[]' for nil:NilClass

其他部分错误输出:

Starting Chef Client, version 11.4.4
Compiling Cookbooks...
Converging 3 resources
Recipe: nginx::default
  * package[nginx] action install (up to date)
  * service[nginx] action enable (up to date)
  * service[nginx] action start (up to date)
  * template[/etc/nginx/nginx.conf] action create
================================================================================
Error executing action `create` on resource 'template[/etc/nginx/nginx.conf]'
================================================================================


Chef::Mixin::Template::TemplateError
------------------------------------
undefined method `[]' for nil:NilClass


Resource Declaration:
---------------------
# In /cookbooks/nginx/recipes/default.rb

  8: template "/etc/nginx/nginx.conf" do
  9:    notifies :reload, "service[nginx]"
 10: end



Compiled Resource:
------------------
# Declared in /cookbooks/nginx/recipes/default.rb:8:in `from_file'

template("/etc/nginx/nginx.conf") do
  provider Chef::Provider::Template
  action "create"
  retries 0
  retry_delay 2
  path "/etc/nginx/nginx.conf"
  backup 5
  source "nginx.conf.erb"
  cookbook_name :nginx
  recipe_name "default"
end

【问题讨论】:

  • 我认为这与厨师 10 到 11 的变化有关——属性的处理方式略有不同。我相信 Peepcode 是使用 chef 10 创建的,可以先尝试使用 chef 10,然后再切换到 11?

标签: ruby-on-rails variables chef-infra chef-solo


【解决方案1】:

您可以在模板中访问对象变量(以@开头的变量),前提是您通过模板的variables方法传递它们:

template("/etc/nginx/nginx.conf") do
  [...]
  variables( :my_var => node )
  [...]
end

然后您将在模板中使用@my_var。但是您不必传递node,因为它已经在模板中可用。您只需要访问它而不是作为对象变量。模板中的以下代码应该可以工作。

<%= node[:nginx][:worker_processes] %>

只需从node 前面删除@

【讨论】:

  • 在 Chef 11 中必须设置优先级。这就是问题所在。不是@符号或模板变量。
  • 你说得对,Draco,对不起我的困惑。由于某种原因,我的虚拟机也没有正确接收更改。删除 @ 符号现在有效。
【解决方案2】:

node 对象无法通过实例变量(以 @ 符号开头的事物)访问。相反,它是当前上下文中的一个方法。

变化:

<%= @node[:nginx][:worker_processes] %>

<%= node[:nginx][:worker_processes] %>

注意到@符号被删除了吗?通过variables 参数将变量传递给模板时,您只需要@-符号。

【讨论】:

  • 正如我上面所说,这并不能解决问题;问题是在 Chef 11 中,优先级必须设置为
  • 由于某种奇怪的原因,它现在似乎正在按照您的说法工作。我在发行说明中阅读的是隐式写入,而不是读取。所以,稍等片刻后,吃不起眼的馅饼。
  • 赛斯的回答对我有用。顺便说一句,我错误地使用了&lt;% = ... 而不是&lt;%= ...
猜你喜欢
  • 1970-01-01
  • 2016-11-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多