【问题标题】:Replacing a template in a wrapper cookbook替换包装说明书中的模板
【发布时间】:2013-04-02 17:25:09
【问题描述】:

我正在尝试为 chef graphite repo 编写包装食谱

在配方 carbon.rb 中,出现以下几行:

template "#{node['graphite']['base_dir']}/conf/storage-schemas.conf" do
  owner node['apache']['user']
  group node['apache']['group']
end

templates/default/storage-schemas.conf 中有一个我不喜欢的 storage-schemas.conf 文件。我可以内联编辑文件并实现我想要的,但如果我希望能够在不发生合并冲突的情况下使我的 repo 保持最新,这似乎不是一个好的厨师实践。所以我想知道我是否可以用包装食谱来解决这个问题。

我的第一个想法是这样的

include_recipe "graphite"
template "#{node['graphite']['base_dir']}/conf/storage-schemas.conf" do
  owner node['apache']['user']
  group node['apache']['group']
end

我会在基本配方完成后重新运行命令并将我想要的文件放在 wrappercookbook/templates/storage-schemas.conf.erb 中。这是一种常见的做法吗?感觉不是很干,但我想不出更干净的方法。

【问题讨论】:

    标签: chef-infra


    【解决方案1】:

    你已经很接近了。假设您的包装说明书中有 storage-schemas.conf.erb 文件的修改版本,您可以这样做:

    include_recipe "graphite"
    begin
      r = resources(:template => "#{node['graphite']['base_dir']}/conf/storage-schemas.conf")
      r.cookbook "my-cookbook"
    rescue Chef::Exceptions::ResourceNotFound
      Chef::Log.warn "could not find template to override!"
    end
    

    你也可以使用这样的行:

    r.source "graphite-stuff/my-storage-schemas.conf.erb"
    

    如果您想以不同的方式组织包装说明书中的文件。

    【讨论】:

    • 在 chef-solo 中似乎不起作用,抱怨尽管包含基本配方,但找不到资源。
    【解决方案2】:

    作为 Dave 答案的替代方案,您还可以使用 chef-rewind

    https://github.com/bryanwb/chef-rewind

    来自 github repo 的快速使用示例

    # 文件 postgresql/recipes/server.rb

    template "/var/pgsql/data/postgresql.conf" do
      source  "postgresql.conf.erb"
      owner "postgres"
    end
    

    # 文件 my-postgresql/recipes/server.rb

    chef_gem "chef-rewind"
    require 'chef/rewind'
    
    include_recipe "postgresql::server"
    # my-postgresql.conf.erb located inside my-postgresql/templates/default/my-postgresql.conf.erb
    rewind :template => "/var/pgsql/data/postgresql.conf" do
      source "my-postgresql.conf.erb"
      cookbook_name "my-postgresql"
    end
    

    【讨论】:

      【解决方案3】:

      在使用knife 时,建议您制作补丁并与上游合并,因为knife 会自动为您执行一些git 分支合并内容,您可以跟踪最初更改的内容。

      简单地覆盖你的包装食谱中的文件是我之前没有遇到的一种做法,但看起来很有趣 ^^ 缺点:你必须手动维护上游更改并将其合并到修改后的模板中,这有时可能比让 git 做更多的工作给你的东西。

      第三种方式:依赖“cookbook shadowing”(已弃用),当您可以直接控制最终用户将使用哪些cookbook 时有效:http://tickets.opscode.com/browse/CHEF-2308

      【讨论】:

        【解决方案4】:

        使用 chef 12,您可以使用 edit_resource

        include_recipe 'communitycookbook'
        
        edit_resource!(:template, '/etc/myapp.conf') do
          source 'other.erb'
          cookbook 'wrapper'
          variables.update(port: 8080)
        end
        

        您可以在这里找到更多相关信息:https://coderanger.net/rewind/

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-09-25
          • 1970-01-01
          • 1970-01-01
          • 2021-10-30
          • 2019-09-04
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多