gpt4 book ai didi

ruby-on-rails - Chef 模板变量@node 似乎抛出错误

转载 作者:行者123 更新时间:2023-12-04 11:39:30 26 4
gpt4 key购买 nike

我正在阅读 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

最佳答案

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

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

那么您将拥有 @my_var在模板中可用。但您不必通过 node ,因为它已经在模板中可用。您只需要访问它而不是作为对象变量。模板中的以下代码应该可以工作。
<%= node[:nginx][:worker_processes] %>

只需删除 @从前面 node .

关于ruby-on-rails - Chef 模板变量@node 似乎抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17642098/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com