如何将参数传递给厨师模板?
我刚开始学习厨师。如今,我正在测试 - https://docs.chef.io /resources/template/
但是我一直都失败了...这是我的代码 -
- 我创建了一个名为示例的食谱,并创建了一个名为
default.rb
的食谱,
file '/srv/www/htdocs/index.html' do
content 'Hello World!'
end
include_recipe '::e'
- 然后我创建了另一个食谱
e.rb
-
default['authorization']['sudo']['groups'] = %w(sysadmin wheel admin)
default['authorization']['sudo']['users'] = %w(jerry greg)
template '/tmp/test.txt' do
source 'test.txt.erb'
mode '0440'
owner 'root'
group 'root'
variables(
sudoers_groups: node['authorization']['sudo']['groups'],
sudoers_users: node['authorization']['sudo']['users']
)
end
- 在此食谱的模板文件夹中,我创建了一个ERB文件 -
test.txt.erb
Defaults !lecture,tty_tickets,!fqdn
root ALL=(ALL) ALL
<% @sudoers_users.each do |user| -%>
<%= user %> ALL=(ALL) <%= "NOPASSWD:" if @passwordless %>ALL
<% end -%>
%sysadmin ALL=(ALL) <%= "NOPASSWD:" if @passwordless %>ALL
<% @sudoers_groups.each do |group| -%>
<%= group %> ALL=(ALL) <%= "NOPASSWD:" if @passwordless %>ALL
<% end -%>
- code quot off'' -
[2022-05-06T18:01:07+08:00] FATAL: NameError: undefined local variable or method `default' for cookbook: sample, recipe: e :Chef::Recipe
- 由于错误显示的错误找不到名为“默认”的变量,在该示例中,它使用
node ['授权'] ['sudo'] ['组']
将参数传递给sudoers_groups,我认为e.rb可能应该是这个 -
node['authorization']['sudo']['groups'] = %w(sysadmin wheel admin)
node['authorization']['sudo']['users'] = %w(jerry greg)
template '/tmp/test.txt' do
source 'test.txt.erb'
mode '0440'
owner 'root'
group 'root'
variables(
sudoers_groups: node['authorization']['sudo']['groups'],
sudoers_users: node['authorization']['sudo']['users']
)
end
- 但仍然失败 -
[2022-05-06T17:39:38+08:00] FATAL: NoMethodError: undefined method `[]' for nil:NilClass
我真的被这个官方样本弄乱了。请帮助我,事先感谢您的任何想法。
问候 艾森
I just started to study chef. These days, I'm testing the sample of templates on -- https://docs.chef.io/resources/template/
But I failed all the times ... Here's my code --
- I created a cookbook named sample, and created a recipe named
default.rb
file '/srv/www/htdocs/index.html' do
content 'Hello World!'
end
include_recipe '::e'
- Then I created another recipe
e.rb
--
default['authorization']['sudo']['groups'] = %w(sysadmin wheel admin)
default['authorization']['sudo']['users'] = %w(jerry greg)
template '/tmp/test.txt' do
source 'test.txt.erb'
mode '0440'
owner 'root'
group 'root'
variables(
sudoers_groups: node['authorization']['sudo']['groups'],
sudoers_users: node['authorization']['sudo']['users']
)
end
- In this cookbook's templates folder, I created a erb file --
test.txt.erb
Defaults !lecture,tty_tickets,!fqdn
root ALL=(ALL) ALL
<% @sudoers_users.each do |user| -%>
<%= user %> ALL=(ALL) <%= "NOPASSWD:" if @passwordless %>ALL
<% end -%>
%sysadmin ALL=(ALL) <%= "NOPASSWD:" if @passwordless %>ALL
<% @sudoers_groups.each do |group| -%>
<%= group %> ALL=(ALL) <%= "NOPASSWD:" if @passwordless %>ALL
<% end -%>
- Then after kick off "chef-client" , error message shows --
[2022-05-06T18:01:07+08:00] FATAL: NameError: undefined local variable or method `default' for cookbook: sample, recipe: e :Chef::Recipe
- Since error shows can't find the variable named 'default' and in that sample -- it's using
node['authorization']['sudo']['groups']
to pass parameters to sudoers_groups, I think the e.rb maybe should be this --
node['authorization']['sudo']['groups'] = %w(sysadmin wheel admin)
node['authorization']['sudo']['users'] = %w(jerry greg)
template '/tmp/test.txt' do
source 'test.txt.erb'
mode '0440'
owner 'root'
group 'root'
variables(
sudoers_groups: node['authorization']['sudo']['groups'],
sudoers_users: node['authorization']['sudo']['users']
)
end
- But it still fails --
[2022-05-06T17:39:38+08:00] FATAL: NoMethodError: undefined method `[]' for nil:NilClass
I really messed up by this official sample. Please kind help me, Thanks in advance for any ideas.
Regards
Eisen
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在许多地方可以定义属性。出于此答案的目的,我们将其限制为食谱和属性文件。有不同的先验规则用于属性。
在cookbook的属性文件中定义,例如
示例/属性/default.rb
,默认
precepence:然后,食谱
sample/e.rb
和模板test.txt.erb
可以按照您在问题中所描述的方式使用。需要使用语法
node。
但是,当我们定义配方中的属性时,我们 /E.RB :
There are many places where attributes can be defined. For the purpose of this answer, we'll limit it to the recipe, and the attributes file. There are different precedence rules for attributes.
Defining in cookbook's attributes file, such as
sample/attributes/default.rb
with thedefault
precedence:Then the recipe
sample/recipes/e.rb
and templatetest.txt.erb
could be used as you described in your question.But when we define the attributes in recipe, we need to use the syntax
node.<precedence>
, such asnode.default
:Defined in
sample/recipes/e.rb
: