使用 puppet 将文本添加到文件的第一行
Add text to the first line of a file with puppet
有没有办法在 Puppet 文件的第一行添加一行(或更好的几行)新文本? (显然让它只做一次)
背景是,我正在管理文件中的某些行,但我想在文件顶部放置一次性注释,因此该文件的清晰部分是 "managed"。
我认为您无法使用 file_line 或 augeas 等标准人偶资源来做到这一点。你可以做的是使用 exec fx [edited]:
$file = /somefile,
$text = 'some text'
) {
exec { "add ${text} to ${file}":
command => "sed -i '1s/^/${text}\n/' '${file}'",
unless => "grep '${text}' '${file}'",
path => ['/bin'],
}
}
有关使用 bash 的其他示例,请参见 How do I add text to the beginning of a file in Bash?。
[发布的原始语法]
该示例已更新,建议来自 [anonymous] 以支持文件名中的空格和双转义新行。由于我没有测试过换行符的双重转义,所以保留下面的原始语法以供参考。
$file = /somefile,
$text = 'some text'
) {
exec { "add ${text} to ${file}":
command => "sed -i '1s/^/${text}\n/' ${file}",
unless => "grep '${text}' ${file}",
path => ['bin'],
}
}
有没有办法在 Puppet 文件的第一行添加一行(或更好的几行)新文本? (显然让它只做一次)
背景是,我正在管理文件中的某些行,但我想在文件顶部放置一次性注释,因此该文件的清晰部分是 "managed"。
我认为您无法使用 file_line 或 augeas 等标准人偶资源来做到这一点。你可以做的是使用 exec fx [edited]:
$file = /somefile,
$text = 'some text'
) {
exec { "add ${text} to ${file}":
command => "sed -i '1s/^/${text}\n/' '${file}'",
unless => "grep '${text}' '${file}'",
path => ['/bin'],
}
}
有关使用 bash 的其他示例,请参见 How do I add text to the beginning of a file in Bash?。
[发布的原始语法] 该示例已更新,建议来自 [anonymous] 以支持文件名中的空格和双转义新行。由于我没有测试过换行符的双重转义,所以保留下面的原始语法以供参考。
$file = /somefile,
$text = 'some text'
) {
exec { "add ${text} to ${file}":
command => "sed -i '1s/^/${text}\n/' ${file}",
unless => "grep '${text}' ${file}",
path => ['bin'],
}
}