Ruby 如何使用chef recipe在不删除原始文件内容的情况下使用多行更新文件
我想在文件/var/rsyslog.conf中附加以下行,而不必删除现有文件 要包含在文件中的行包括Ruby 如何使用chef recipe在不删除原始文件内容的情况下使用多行更新文件,ruby,chef-infra,cookbook,Ruby,Chef Infra,Cookbook,我想在文件/var/rsyslog.conf中附加以下行,而不必删除现有文件 要包含在文件中的行包括 ************* #audit log $ModLoad imfile $InputFileName /var/log/audit/audit.log $InputFileTag tag_audit_log: $InputFileStateFile audit_log $InputFileSeverity info $InputFileFacility local6 $InputRun
*************
#audit log
$ModLoad imfile
$InputFileName /var/log/audit/audit.log
$InputFileTag tag_audit_log:
$InputFileStateFile audit_log
$InputFileSeverity info
$InputFileFacility local6
$InputRunFileMonitor
*.* @@172.167.189.67:514
*************
在配方中,我将以下内容作为文件参考
****
file '/etc/rsyslog.conf' do
content ' #audit log
$ModLoad imfile
$InputFileName /var/log/audit/audit.log
$InputFileTag tag_audit_log:
$InputFileStateFile audit_log
$InputFileSeverity info
$InputFileFacility local6
$InputRunFileMonitor'
*.* @@172.167.189.67:514 --> #This value needs to be dynamically changed using String Intepolation
mode '0644'
owner 'root'
group 'root'
end
****
虽然它更新了文件,但它只有上面几行,所有其他文件内容现在都消失了
我尝试创建一个扩展名为.erb的新模板文件,顺便说一句,扩展名也是.erb。插入内容,但删除较旧的文件内容
将文件与属性值插值一起附加的建议方法是什么
用例:
Attribute.rb文件
默认值['serverIP']['hostname']=“172.167.189.67:514”
此属性值将是动态的,并将定期更改
我想在文件或模板中使用插值,以便它拾取属性文件中提供的值
实现这一点最简单的方法是什么
谢谢正如您所注意到的,
文件
资源用于立即管理整个文件。我们建议不要进行部分文件更新,因为这会导致非常脆弱的Chef core,但如果必须,请查看poise文件
和行
食谱。正如@coderander提到的,不建议在Chef中进行部分文件更新,因为在Chef中管理整个文件会更好
但是,如果必须这样做(有时是这样),您可以通过以下方式实现:
bash 'append line(s) to file if it doesnt exist' do
user 'user'
code <<-EOS
cat >>/home/file <<EOL
*.* @@172.167.189.67:514
EOL
EOS
not_if "grep -q 172.167.189.67 /home/file"
end
bash“如果文件不存在,则将行追加到文件中”执行
用户“用户”
代码/主页/文件