Kit*_* Ho 26 coding-style puppet
在puppet中,如果define命令> 80个字符,我怎么能用两行来包含它呢?
exec { 'create_domain':
command => "some command exceed 80 character...........................................................how to do how to do?.......",
}
Run Code Online (Sandbox Code Playgroud)
pwa*_*wan 19
这有点难看,但如果字符串中的最后一个字符是'\'后跟换行符,那么该字符串将在下一行继续.我的sample.pp清单如下:
class test {
exec { 'create_domain':
command => "/bin/echo 1234567890123456789012345678901234567890123456789012345678901234567890\
wrapped > /var/tmp/test.txt";
}
}
node 'pwan-central' {
include test
}
Run Code Online (Sandbox Code Playgroud)
使用Puppet 2.7.1在Ubuntu 11.10上使用'puppet apply sample.pp'运行此命令会得到以下输出
mrpwan@pwan-central:~$ puppet apply sample.pp
notice: /Stage[main]/Test/Exec[create_domain]/returns: executed successfully
notice: Finished catalog run in 0.10 seconds
Run Code Online (Sandbox Code Playgroud)
捕获创建的文件显示行已包装:
mrpwan@pwan-central:~$ cat /var/tmp/test.txt
1234567890123456789012345678901234567890123456789012345678901234567890wrapped
Run Code Online (Sandbox Code Playgroud)
请参阅https://github.com/puppetlabs/puppet/blob/9fbb36de/lib/puppet/parser/lexer.rb#L537(自Puppet v2.7.0起)
这也是一个已知问题:http://projects.puppetlabs.com/issues/5022
小智 7
从Puppet 3.5开始,你有两个我用过的选项.Ruby允许您通过几行连接字符串.
string = "line #1"\
"line #2"\
"line #3"
p string # => "line #1line #2line #3"
Run Code Online (Sandbox Code Playgroud)
另一个选择,从Puppet 3.5开始,他们添加了HereDoc功能.这将允许您将字符串放在源代码文件的一部分中,该部分文件被视为单独的文件.
$mytext = @(EOT)
This block of text is
visibly separated from
everything around it.
| EOT
Run Code Online (Sandbox Code Playgroud)
puppet文档在这里:https://docs.puppet.com/puppet/4.9/lang_data_string.html#heredocs
对于大块数据,heredocs是处理Puppet清单中的长行的最佳方法。该/L插补选项特别有用。/L导致\行尾删除换行符。例如,以下代码可以满足您的期望,去除缩进和换行符,包括结尾的换行符。
sshkey { 'example.com':
ensure => present,
type => 'ssh-rsa',
key => @(KEY/L),
RfrXBrU1T6qMNllnhXsJdaud9yBgWWm6OprdEQ3rpkTvCc9kJKH0k8MNfKxeBiGZVsUn435q\
e83opnamtGBz17gUOrzjfmpRuBaDDGmGGTPcO8Dohwz1zYuir93bJmxkNldjogbjAWPfrX10\
8aoDw26K12sK61lOt6GTdR9yjDPdG4zL5G3ZjXCuDyQ6mzcNHdAPPFRQdlRRyCtG2sQWpWan\
3AlYe6h6bG48thlo6vyNvOD8s9K0YBnwl596DJiNCY6EsxnSAhA3Uf9jeKqlVqqrxhEzHufx\
07iP1nXIXCMUV
|-KEY
target => '/home/user/.ssh/known_hosts',
}
Run Code Online (Sandbox Code Playgroud)
小智 5
如果您真的关心 80 列限制,您可以随时滥用模板来实现该目标
exec {'VeryLongExec':
command => template("${module}/verylongexec")
}
Run Code Online (Sandbox Code Playgroud)
然后将实际命令放入该模板文件中
学分应由 Jan Vansteenkiste 计算