I'm going through peepcode's Chef tutorial, so far so good. For some reason there is a failure when using template variables. The example is for nginx.
我正在通过peepcode的Chef教程,到目前为止一切顺利。由于某种原因,使用模板变量时出现故障。这个例子是针对nginx的。
In the nginx/attributes/nginx.rb I have:
在nginx / attributes / nginx.rb中我有:
default[:nginx][:worker_processes] = 4
In the nginx.conf.erb template I refer to:
在nginx.conf.erb模板中我指的是:
worker_processes <%= @node[:nginx][:worker_processes] %>;
Below is the error I get running chef-solo:
以下是我运行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
Other part of the error output:
错误输出的其他部分:
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
2 个解决方案
#1
6
You can access object variables (the ones that start with @) in templates, only if you passed them through variables
method of template like that:
您可以在模板中访问对象变量(以@开头的变量),只有当您通过模板的变量方法传递它们时才会这样:
template("/etc/nginx/nginx.conf") do
[...]
variables( :my_var => node )
[...]
end
Then you will have @my_var
available in template. But you don't have to pass the node
, because it is already available in templates. You just have to access it not as an object variable. The following code in template should work.
然后你将在模板中提供@my_var。但您不必传递节点,因为它已在模板中可用。您只需要访问它而不是对象变量。模板中的以下代码应该有效。
<%= node[:nginx][:worker_processes] %>
Just remove the @
from the front of node
.
只需从节点前面删除@。
#2
2
The node
object is not accessible via an instance variable (the thing starting with an @ sign). Instead, it is a method in the current context.
无法通过实例变量(以@符号开头的东西)访问节点对象。相反,它是当前上下文中的方法。
Change:
<%= @node[:nginx][:worker_processes] %>
to
<%= node[:nginx][:worker_processes] %>
Notice the removal of the @-sign? You only need the @-sign when passing in variables to the template via the variables
parameter.
请注意删除@ -sign?通过变量参数将变量传递给模板时,只需要@ -sign。
#1
6
You can access object variables (the ones that start with @) in templates, only if you passed them through variables
method of template like that:
您可以在模板中访问对象变量(以@开头的变量),只有当您通过模板的变量方法传递它们时才会这样:
template("/etc/nginx/nginx.conf") do
[...]
variables( :my_var => node )
[...]
end
Then you will have @my_var
available in template. But you don't have to pass the node
, because it is already available in templates. You just have to access it not as an object variable. The following code in template should work.
然后你将在模板中提供@my_var。但您不必传递节点,因为它已在模板中可用。您只需要访问它而不是对象变量。模板中的以下代码应该有效。
<%= node[:nginx][:worker_processes] %>
Just remove the @
from the front of node
.
只需从节点前面删除@。
#2
2
The node
object is not accessible via an instance variable (the thing starting with an @ sign). Instead, it is a method in the current context.
无法通过实例变量(以@符号开头的东西)访问节点对象。相反,它是当前上下文中的方法。
Change:
<%= @node[:nginx][:worker_processes] %>
to
<%= node[:nginx][:worker_processes] %>
Notice the removal of the @-sign? You only need the @-sign when passing in variables to the template via the variables
parameter.
请注意删除@ -sign?通过变量参数将变量传递给模板时,只需要@ -sign。