如何将参数传递给厨师模板?

发布于 2025-01-27 02:49:57 字数 2425 浏览 2 评论 0原文

我刚开始学习厨师。如今,我正在测试 - https://docs.chef.io /resources/template/

但是我一直都失败了...这是我的代码 -

  1. 我创建了一个名为示例的食谱,并创建了一个名为default.rb的食谱,
file '/srv/www/htdocs/index.html' do
  content 'Hello World!'
end

include_recipe '::e'
  1. 然后我创建了另一个食谱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
  1. 在此食谱的模板文件夹中,我创建了一个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 -%>

  1. 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
  1. 由于错误显示的错误找不到名为“默认”的变量,在该示例中,它使用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
  1. 但仍然失败 -
[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 --

  1. 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'
  1. 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
  1. 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 -%>

  1. 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
  1. 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
  1. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

贱贱哒 2025-02-03 02:49:57

在许多地方可以定义属性。出于此答案的目的,我们将其限制为食谱和属性文件。有不同的先验规则用于属性。

在cookbook的属性文件中定义,例如示例/属性/default.rb默认 precepence:

default['authorization']['sudo']['groups'] = %w(sysadmin wheel admin)
default['authorization']['sudo']['users'] = %w(jerry greg)

然后,食谱sample/e.rb和模板test.txt.erb可以按照您在问题中所描述的方式使用。

需要使用语法node。

但是,当我们定义配方中的属性时,我们 /E.RB :

node.default['authorization']['sudo']['groups'] = %w(sysadmin wheel admin)
node.default['authorization']['sudo']['users'] = %w(jerry greg)

template '/tmp/test.txt' do
# and so on

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 the default precedence:

default['authorization']['sudo']['groups'] = %w(sysadmin wheel admin)
default['authorization']['sudo']['users'] = %w(jerry greg)

Then the recipe sample/recipes/e.rb and template test.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 as node.default:

Defined in sample/recipes/e.rb:

node.default['authorization']['sudo']['groups'] = %w(sysadmin wheel admin)
node.default['authorization']['sudo']['users'] = %w(jerry greg)

template '/tmp/test.txt' do
# and so on
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文