gpt4 book ai didi

ruby - 使用多个 Chef Recipe 编写文件,所有这些 Recipe 都针对同一个文件

转载 作者:数据小太阳 更新时间:2023-10-29 08:03:38 25 4
gpt4 key购买 nike

我的情况是我有三本 Recipe ,每本都有一个写入/etc/hosts 文件的模板资源。

与其覆盖,我想附加:

  • 第一本 Recipe 创建/etc/hosts 文件并写入第 1、2、3 行。
  • 第二本 Recipe 附加了第 4,5 行。等

在 Chef 领域处理这个问题的正确方法是什么?

最佳答案

您最好创建一个 Recipe 来管理从属性生成的文件。

CookbookA/attributes/default.rb

default['hosts']['lines'] = []

CookbookA/recipes/genfile.rb

template "/etc/hosts" do
source "hosts.erb"
end

CookbookA/templates/default/hosts.erb

#File Generated by Chef
<% node['hosts']['lines'].each do |l| %>
<% l -%>
<% end.unless node['hosts']['lines'].empty? %>

然后在您的其他 Recipe 属性文件中:

default['hosts']['lines'] << ["first line","second line"]

并且有一些 Recipe 取决于 CookbookA 并且在他们的 Recipe 中调用 include_recipe "CookbookA::genfile.rb"

使用 <<您附加到属性而不是覆盖它们。

关于ruby - 使用多个 Chef Recipe 编写文件,所有这些 Recipe 都针对同一个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30925368/

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