Linux 使用 Chef 配方将多行附加到配置文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16879469/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using a Chef recipe to append multiple lines to a config file
提问by Gui Ambros
I'm trying to create a Chef recipe to append multiple lines (20-30) to a specific config file.
我正在尝试创建一个 Chef 配方以将多行 (20-30) 附加到特定的配置文件。
I'm aware the recommended pattern is to change entire config filesrather than just appending to a file, but I dislike this approach for multiple reasons.
我知道推荐的模式是更改整个配置文件而不是仅仅附加到文件,但由于多种原因我不喜欢这种方法。
So far the only solution I found was to use a cookbook_file
and then use a bash resource to do:
到目前为止,我找到的唯一解决方案是使用 acookbook_file
然后使用 bash 资源来执行以下操作:
cat lines_to_append >> /path/configfile
cat lines_to_append >> /path/configfile
Obviously this wouldn't work properly, as it'd append the file over and over, each time you run chef-client. I'd have to create a small bash script to check for a specific string first, and, if not found, append to the file.
显然这不会正常工作,因为它会一遍又一遍地附加文件,每次运行厨师客户端。我必须先创建一个小的 bash 脚本来检查特定的字符串,如果没有找到,则附加到文件中。
But this seems to defeat the purpose of using Chef. There must be a better way.
但这似乎违背了使用 Chef 的初衷。一定会有更好的办法。
One promising solution was the line cookbookfrom OpsCode Community. It aimed to solve this exact problem. Unfortunately the functionality is incomplete, buggy, and the code is just a quick hack. Far from being a solid solution.
一个很有前途的解决方案是来自 OpsCode 社区的行食谱。它旨在解决这个确切的问题。不幸的是,功能不完整,有问题,代码只是一个快速的黑客。远不是一个可靠的解决方案。
Another option I evaluated was augeas. Seems pretty powerful, but it'd add yet-another layer of abstraction to the system. Overkill, in my case.
我评估的另一个选项是augeas。看起来非常强大,但它会为系统添加另一层抽象。矫枉过正,就我而言。
Given that this is one of the most obvious tasks for any sysadmin, is there any easy and beautiful solution with Chef that I'm not seeing?
鉴于这是任何系统管理员最明显的任务之一,是否有我没有看到的 Chef 的任何简单而漂亮的解决方案?
EDIT: here's how I'm solving it so far:
编辑:这是我目前解决的方法:
cookbook_file "/tmp/parms_to_append.conf" do
source "parms_to_append.conf"
end
bash "append_to_config" do
user "root"
code <<-EOF
cat /tmp/parms_to_append.conf >> /etc/config
rm /tmp/parms_to_append.conf
EOF
not_if "grep -q MY_IDENTIFIER /etc/config"
end
It works, but not sure this is the recommended Chef pattern.
它有效,但不确定这是推荐的 Chef 模式。
采纳答案by cassianoleal
As you said yourself, the recommended Chef pattern is to manage the whole file.
正如您自己所说,推荐的 Chef 模式是管理整个文件。
If you're using Chef 11 you could probably make use of partials for what you're trying to achieve.
如果您使用的是 Chef 11,您可能可以使用部分来实现您想要实现的目标。
There's more info hereand on this example cookbook.
As long as you have access to the original config template, just append <%= render "original_config.erb" %>
to the top of your parms_to_append.conf
template.
只要您可以访问原始配置模板,只需将其附加<%= render "original_config.erb" %>
到parms_to_append.conf
模板的顶部。
回答by Konzulic
As said before, using templates and partialsis common way of doing this, but chef allows appending files, and even changing(editing) file lines. Appendind is performed using following functions:
如前所述,使用模板和部分是执行此操作的常用方法,但 Chef 允许附加文件,甚至更改(编辑)文件行。Appendind 使用以下函数执行:
- insert_line_after_match(regex, newline);
- insert_line_if_no_match(regex, newline)
- insert_line_after_match(正则表达式,换行符);
- insert_line_if_no_match(正则表达式,换行符)
You may find and example hereon stackoverflow, and the full documentation on rubydoc.info
您可能会发现和例如这里的计算器,以及完整的文档rubydoc.info
Please use it with caution, and only when partials and templates are not appropriate.
请谨慎使用,仅当部分和模板不合适时。
回答by Kasper Grubbe
I did something like this:
我做了这样的事情:
monit_overwrites/templates/default/monitrc.erb:
monit_overwrites/templates/default/monitrc.erb:
#---FLOWDOCK-START
set mail-format { from: [email protected] }
#---FLOWDOCK-END
In my recipe I did this:
在我的食谱中,我这样做了:
monit_overwrites/recipes/default.rb:
monit_overwrites/recipes/default.rb:
execute "Clean up monitrc from earlier runs" do
user "root"
command "sed '/#---FLOWDOCK-START/,/#---FLOWDOCK-END/d' > /etc/monitrc"
end
template "/tmp/monitrc_append.conf" do
source "monitrc_append.erb"
end
execute "Setup monit to push notifications into flowdock" do
user "root"
command "cat /tmp/monitrc_append.conf >> /etc/monitrc"
end
execute "Remove monitrc_append" do
command "rm /tmp/monitrc_append.conf"
end
回答by ABCD
The easiest way to tackle this would be to create a string and pass it to content. Of course bash blocks work... but I think file resources are elegant.
解决这个问题的最简单方法是创建一个字符串并将其传递给内容。当然 bash 块工作......但我认为文件资源很优雅。
lines = ""
File.open('input file') do |f|
f.lines.each do |line|
lines = lines + line + "\n"
end
end
file "file path" do
content line
end
回答by Ranjith Kumar G
Here is the example ruby block for inserting 2 new lines after match:
这是在匹配后插入 2 个新行的示例 ruby 块:
ruby_block "insert_lines" do
block do
file = Chef::Util::FileEdit.new("/etc/nginx/nginx.conf")
file.insert_line_after_match("worker_rlimit_nofile", "load_module 1")
file.insert_line_after_match("pid", "load_module 2")
file.write_file
end
end
insert_line_after_match
searches for the regex/string and it will insert the value in after the match.
insert_line_after_match
搜索正则表达式/字符串,它将在匹配后插入值。