如何使用puppet模块对文件中所有未注释的行进行注释



我有一个配置文件,其中包含注释行和非注释行。我想使用puppet对该文件中所有未注释的行进行注释。有什么最佳/简单的方法可以做到这一点吗?或者有没有一种方法可以通过puppet运行bash命令(可能是sed来替换)?我不确定使用bash命令是否是正确的方法。

如果有人指导我做这件事,那真的会很有帮助。提前感谢!

有什么最佳/简单的方法可以做到这一点吗?

没有内置的资源类型或众所周知的模块专门确保文件的非空行以#字符开头。

或者有没有一种方法可以通过puppet运行bash命令(可能是sed来替换)?

是,Exec资源类型。这是除了编写自定义资源类型之外的最佳选择。

我不确定使用bash命令是否是正确的方法。

在一般意义上,它不是。适当的、特定的资源类型比Exec更好。但当你没有合适的,也懒得做的时候,Exec是可用的。

它可能看起来像这样:

# The file to work with, so that we don't have to repeat ourselves
$target_file = '/etc/ssh/sshd_config'
exec { "Comment uncommented ${target_file} lines":
# Specifying the command in array form avoids complicated quoting or any
# risk of Puppet word-splitting the command incorrectly
command  => ['sed', '-i', '-e', '/^[[:space:]]*[^#]/ s/^/# /', $target_file],
# If we didn't specify a search path then we would need to use fully-qualified
# command names in 'command' above and 'onlyif' below
path     => ['/bin', '/usr/bin', '/sbin', '/usr/sbin'],
# The file needs to be modified only if it contains any non-blank, uncommented
# lines.  Testing that via an 'onlyif' ensures that Puppet will not
# run 'sed' or (more importantly) report the file changed when it does
# not initially contain any lines that need to be commented
onlyif   => [['grep', '-q', '^[[:space:]]*[^#]', $target_file]],
# This is the default provider for any target node where the rest of this
# resource would work anyway.  Specifying it explicitly will lead to a more
# informative diagnostic if there is an attempt to apply this resource to
# a system to which it is unsuited.
provider => 'posix',
}

这并不依赖于bash或任何其他shell来运行命令,但它确实依赖于指定目录中的sedgrep。事实上,它特别依赖于GNUsed或支持具有相同语义的-i选项。值得注意的是,这并不包括BSD风格的sed,比如您在macOS上可以找到的。

最新更新