gpt4 book ai didi

ruby-on-rails - 在 Recipe 中访问 Chef 数据包

转载 作者:行者123 更新时间:2023-12-04 22:57:51 25 4
gpt4 key购买 nike

我创建了以下数据包项目:

{
"name": "data_bag_item_nameservers_servers",
"json_class": "Chef::DataBagItem",
"chef_type": "data_bag_item",
"data_bag": "nameservers",
"raw_data": {
"id": "servers",
"serverslist": [
"xxx.xxx.xxx.xxx",
"xxx.xxx.xxx.xxx"
]
}
}

在模板 erb 中,我在电话下方添加了,
<% @serverslist.each_with_index do |nmserver| %>
nameserver <%= nmserver %>
<% end %>

但它对我不起作用,并给出错误,

Error executing action create on resource 'template[/etc/resolve.conf]'

Chef::Mixin::Template::TemplateError

undefined method each_with_index' for nil:NilClass



有人可以帮助我如何在食谱配方中调用这些数据包项目值吗?

提前致谢!

最佳答案

您是否考虑过使用节点属性的更简单的选项?

在运行时很容易覆盖说明书默认设置。我指出这一点是因为根据我的经验很少需要数据包。

我已经包括了两个例子。

示例 1:使用属性

“演示”食谱

├── attributes
│   └── default.rb
├── Berksfile
├── Berksfile.lock
├── chefignore
├── metadata.rb
├── README.md
├── recipes
│   └── default.rb
├── templates
│   └── default
│   └── dummy.erb
└── test
└── integration
├── default
│   └── serverspec
│   └── default_spec.rb
└── helpers
└── serverspec
└── spec_helper.rb

属性/default.rb
default['demo']['nameservers']['one'] = "one"
default['demo']['nameservers']['two'] = "two"
default['demo']['nameservers']['three'] = "three"

食谱/default.rb
template "/etc/dummy" do
source "dummy.erb"
owner 'root'
group 'root'
mode '0644'
end

模板/默认/dummy.erb
<% node['demo']['nameservers'].each do |name,server| %>
nameserver <%= server %>
<% end %>

测试/集成/默认/serverspec/default_spec.rb
require 'spec_helper'

describe file('/etc/dummy') do
it { should be_file }
it { should be_owned_by 'root' }
it { should contain 'nameserver one' }
it { should contain 'nameserver two' }
it { should contain 'nameserver three' }
end

示例 2:使用数据包

测试/集成目录下带有测试数据包的“演示”食谱
├── Berksfile
├── Berksfile.lock
├── chefignore
├── metadata.rb
├── README.md
├── recipes
│   └── default.rb
├── templates
│   └── default
│   └── dummy.erb
└── test
└── integration
├── data_bags
│   └── stuff
│   └── nameservers.json
├── default
│   └── serverspec
│   └── default_spec.rb
└── helpers
└── serverspec
└── spec_helper.rb

测试/集成/data_bags/stuff/nameservers.json

样本数据
{
"id": "nameservers",
"list": [
"one",
"two",
"three"
]
}

食谱/default.rb

现在配方更复杂了。必须显式检索数据包中的数据,然后将其作为变量传递到模板中
nameservers = data_bag_item('stuff', "nameservers")

template "/etc/dummy" do
source "dummy.erb"
owner 'root'
group 'root'
mode '0644'
variables ({
"servers" => nameservers["list"]
})
end

模板/默认/dummy.erb
<% @servers.each do |server| %>
nameserver <%= server %>
<% end %>

测试/集成/默认/serverspec/default_spec.rb
require 'spec_helper'

describe file('/etc/dummy') do
it { should be_file }
it { should be_owned_by 'root' }
it { should contain 'nameserver one' }
it { should contain 'nameserver two' }
it { should contain 'nameserver three' }
end

关于ruby-on-rails - 在 Recipe 中访问 Chef 数据包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37078587/

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