【发布时间】:2015-01-13 08:49:22
【问题描述】:
目前我想从一个参数创建一个 md5 哈希。然后我想将哈希写入文件(路径是另一个参数)。
那是自定义函数:
module Puppet::Parser::Functions
newfunction(:write_line_to_file) do |args|
require 'md5'
filename = args[0]
str = MD5.new(lookupvar(args[1])).to_s
File.open(filename, 'a') {|fd| fd.puts str }
end
end
以及 puppet manifest 中的调用:
write_line_to_file('/tmp/some_hash', "Hello world!")
我得到的结果是一个文件,内容不是散列而是原始字符串。 (在示例中是 Hello World!)
我知道这个自定义函数没有实际用处。我只是想了解 md5 哈希是如何工作的。
---UPD---
新功能(正常工作):
require 'digest'
module Puppet::Parser::Functions
newfunction(:lxwrite_line_to_file) do |args|
filename = args[0]
str = Digest::MD5.hexdigest args[1]
File.open(filename, 'w') {|fd| fd.puts str }
end
end
【问题讨论】: