如何使用厨师食谱多行更新文件而不删除原始文件的内容 [英] How to update the file with multiple lines without deleting the contents of the original file using chef recipe

查看:95
本文介绍了如何使用厨师食谱多行更新文件而不删除原始文件的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在文件/var/rsyslog.conf文件中添加以下行,而不必删除我现有的文件。

I would like to append my file /var/rsyslog.conf file with the following lines without having to delete my existing file.

要包含在文件中的行

*************
#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
*************

在食谱中,我将以下内容作为文件资源

In the recipe I gave the following as a file resourse

****
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
****

尽管它更新了文件,但只有上面几行,现在所有其他文件内容都消失了

Although it updated the file, it only has the above lines and all other file contents are now disappeared

我尝试创建一个扩展名为.erb的新模板文件,顺带一提,它也做了同样的事情。插入内容但删除较旧的文件内容。

I tried creating a new template file with the .erb extension which incidentally also does the same. Inserts the contents but deletes the older file contents.

将文件附加到属性值插值的建议方式是什么。

What would be the suggested way to append the file along with attribute value interpolation.

用例:

Attribute.rb文件

Attribute.rb file

default ['serverIP'] [ 'hostname'] = 172.167.189.67:514
此属性值将是动态的,并且会定期更改。

default['serverIP']['hostname'] = "172.167.189.67:514" This Attribute value will be dynamic and will change periodically.

我想在其中使用插值文件或模板,以便它选择属性文件中提供的值。

I would like to use interpolate this in either the file or template so it picks the value provided in the attribute file.

实现此目标的最简单方法是什么?

What is the easiest way of achieving this???

谢谢

推荐答案

正如@coderander提到的那样,不建议在厨师中这样做,因为这样做更好在主厨中管理整个文件。

as @coderander mentioned it's not recommended to do that in chef, as its better to manage the whole file in Chef.

但是,如果必须这样做(有时是这种情况),您可以通过以下方式实现:

However if you have to, which is sometimes the case, you can achieve it this way:

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

您可能需要运行在该^

you may need to run cookstyle on that ^

这篇关于如何使用厨师食谱多行更新文件而不删除原始文件的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆