如何在厨师运行期间动态编辑模板文件

发布于 2025-01-13 03:29:30 字数 334 浏览 3 评论 0原文

我的厨师食谱中有一个名为“测试模板”的模板文件,在我的厨师食谱中,我正在将模板文件采购并更新为文件“/etc/run/mn.txt”,如下所示:

template 'Test Template' do
  source 'run.config'
  path /etc/run/mn.txt
end

我需要更新此模板文件在调用上述“模板”资源之前,在 Chef 运行期间动态地执行此操作。在调用模板资源之前,如何在我的食谱中编辑模板文件本身?我需要这样做,因为我要向模板文件中添加一些行,如果我随后将这些行添加到此资源块之后创建的文件中,则在下一次厨师客户端运行期间它将恢复为原始模板内容。

I have a template file in my chef cookbook named 'Test Template', In my Chef recipe I am sourcing and updating my template file to a file '/etc/run/mn.txt' as follows:

template 'Test Template' do
  source 'run.config'
  path /etc/run/mn.txt
end

I need to update this template file dynamically during the chef run before the above "template" resource is called. How can I edit the template file itself within my cookbook before the template resource is called? I need to do this because there are lines I am adding to the template file and if I add the lines afterward to the created file after this resource block, it will just get reverted back to the original template contents during the next chef-client run.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

随遇而安 2025-01-20 03:29:30

您知道在执行模板资源之前要在 /etc/run/mn.txt 中包含哪些内容吗?如果是这样,您可以使用变量属性(传递到 Ruby 模板文件的变量的哈希值)将内容推送到 /etc/run/mn.txt 文件中:

template 'Test Template' do
  source 'run.config'
  path /etc/run/mn.txt
  variables(
    line_1: 'something here',
    line_2: node['some_attribute'],
    line_3: a_var
  )
end

然后在您的run.config,您可以在末尾添加类似以下内容:(

<previous contents of run.config>
    
<%= @line_1 %>
<%= @line_2 %>
<%= @line_3 %>

或者您可以向变量参数传递哈希值,并通过模板中的哈希键进行某种循环......以使其更加动态)

Do you know the contents that you want to include in /etc/run/mn.txt before the template resource is executed? If so, you can use the variables property (A Hash of variables that are passed into a Ruby template file) to push the contents into the /etc/run/mn.txt file:

template 'Test Template' do
  source 'run.config'
  path /etc/run/mn.txt
  variables(
    line_1: 'something here',
    line_2: node['some_attribute'],
    line_3: a_var
  )
end

And then in your run.config, you could add to the end something like:

<previous contents of run.config>
    
<%= @line_1 %>
<%= @line_2 %>
<%= @line_3 %>

(Or you could pass the variables parameter a hash, and do some sort of loop through the hash keys in your template.. to make it more dynamic)

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