无法使用puppet访问远程文件

时间:2014-08-18 18:04:58

标签: windows puppet

我似乎无法让木偶高兴地从网络共享中获取文件

    file { "installerfile":
        ensure => file,
        source => '\\drivename\installer.msi',
        path   => 'C:/puppetstuff/installer.msi',
    }

因URI错误而失败。它说驱动器名称是一个错误的主机名:

Debug: Reraising Failed to convert '/installer.msi' to URI: bad component(expected host component): drivename

如何正确访问网络共享?

1 个答案:

答案 0 :(得分:0)

puppet/util进行一些挖掘后,我发现即使使用单引号也需要转义每个斜杠:

source => '\\drivename\installer.msi'

应该是

source => '\\\\drivename\\installer.msi'

凌乱的irb输出如下:

1.9.3-p547 :036 > path = '\\\\drivename\test'
 => "\\\\drivename\\test"
1.9.3-p547 :037 > puts path
\\drivename\test
 => nil
1.9.3-p547 :038 >     params = { :scheme => 'file' }
 => {:scheme=>"file"}
1.9.3-p547 :039 >  path = path.gsub(/\\/, '/')
 => "//drivename/test"
1.9.3-p547 :040 >
1.9.3-p547 :041 >         if unc = /^\/\/([^\/]+)(\/.+)/.match(path)
1.9.3-p547 :042?>           params[:host] = unc[1]
1.9.3-p547 :043?>           path = unc[2]
1.9.3-p547 :044?>         elsif path =~ /^[a-z]:\//i
1.9.3-p547 :045?>           path = '/' + path
1.9.3-p547 :046?>         end
 => "/test"
1.9.3-p547 :047 > params[:path] = URI.escape(path)
 => "/test"
1.9.3-p547 :048 > params
 => {:scheme=>"file", :host=>"drivename", :path=>"/test"}
1.9.3-p547 :049 > URI::Generic.build(params)
 => #<URI::Generic:0x007f8da327e808 URL:file://drivename/test>
1.9.3-p547 :050 > URI::Generic.build(params).host
 => "drivename"
1.9.3-p547 :051 > URI::Generic.build(params).path
 => "/test"

在这种情况下,使用unix样式路径更安全。 puppet/util只是将你的UNC路径转换为unix样式路径。

# Convert a path to a file URI
def path_to_uri(path)
  return unless path
  params = { :scheme => 'file' }
  if Puppet.features.microsoft_windows?
    path = path.gsub(/\\/, '/')
    if unc = /^\/\/([^\/]+)(\/.+)/.match(path)
      params[:host] = unc[1]
      path = unc[2]
    elsif path =~ /^[a-z]:\//i
      path = '/' + path
    end
  end
  params[:path] = URI.escape(path)
  begin
    URI::Generic.build(params)
  rescue => detail
    raise Puppet::Error, "Failed to convert '#{path}' to URI: #{detail}"
  end
end

所以source => '//drivename/installer.msi是避免逃避问题的更安全的方法。

希望这有帮助。