无法从 Puppet 提供商加载 ruby gem

Could not load ruby gem from Puppet provider

自从我从 Puppet 3.8 升级到 Puppet 4.2 后,我的模块出现了一些问题。看起来无法加载 gem 个文件。 这是我收到的错误消息:

Error: Could not autoload puppet/provider/test/test: cannot load such file -- rest-client
Error: Could not autoload puppet/type/test: Could not autoload puppet/provider/test/test: cannot load such file -- rest-client
Error: Evaluation Error: Error while evaluating a Resource Statement, Could not autoload puppet/type/test: Could not autoload puppet/provider/test/test: cannot load such file -- rest-client at /home/vagrant/tmp/manifests/mytest.pp:1:1 on node 

我使用以下命令应用清单:

$ puppet apply manifests/mytest.pp --modulepath=~/tmp/modules/ --debug

清单代码如下:

$ cat manifests/mytest.pp 
test {"mytest": }

提供商代码:

$ cat modules/test/lib/puppet/provider/test/test.rb 
require 'rubygems'
require 'rest-client'

Puppet::Type.type(:test).provide(:test) do
  desc "Just testing"

  def create
    puts "Entered create "
  end

  def destroy
  end

  def exists?
   return false
  end
end

类型代码:

$ cat modules/test/lib/puppet/type/test.rb
Puppet::Type.newtype(:test) do

  @doc = "Just testing."

  ensurable do
    defaultvalues
    defaultto :present
  end

  newparam(:name, :namevar => true) do
    desc 'An arbitrary name used as the identity of the resource.'
  end
end

我正在测试 vagrant Box 的 1.0.2 版 (centos-6.6-64-puppet)

傀儡版:

$ puppet --version
4.2.1

RubyGems 版本:

$ gem --version
1.3.7

rest-client gem 使用 Yum 安装 (rubygem-rest-client.noarch):

$ gem which rest-client
/usr/lib/ruby/gems/1.8/gems/rest-client-1.6.1/lib/rest-client.rb

目录结构:

$ tree
.
├── manifests
│   └── mytest.pp
└── modules
    └── test
        └── lib
            └── puppet
                ├── provider
                │   └── test
                │       └── test.rb
                └── type
                    └── test.rb

你遇到过这个问题吗?知道如何解决吗?

终于找到原因了! Puppet 代理附带了自己的 Ruby 和 Gem 二进制文件,它们位于 /opt/puppetlabs/puppet/bin/

通过执行此命令:

$ facter rubysitedir rubyversion
rubysitedir => /opt/puppetlabs/puppet/lib/ruby/site_ruby/2.1.0
rubyversion => 2.1.6

我注意到它指向的目录与标准位置不同。

$ which ruby
/usr/bin/ruby
$ ruby --version
ruby 1.8.7 (2013-06-27 patchlevel 374) [x86_64-linux]

为了解决这个问题,我使用了 puppet 4 附带的 gem 来安装 rest-client:

sudo /opt/puppetlabs/puppet/bin/gem install rest-client