【问题标题】:Cannot figure out how to write custom type无法弄清楚如何编写自定义类型
【发布时间】:2017-09-27 07:54:06
【问题描述】:

更新:

我正在尝试自学如何编写 Puppet 自定义类型。我看过这个文档:https://docs.puppet.com/puppet/4.10/custom_types.htmlhttps://docs.puppet.com/puppet/4.10/provider_development.html

这是我创建一个自定义类型的人为尝试,该类型简单地接受一个字符串数组并将它们写入文件'/tmp/track-titles.txt'。

这是我的类型代码(modules/hello_world/lib/puppet/type/track_titles.rb):

# blah blah blah
Puppet::Type.newtype(:track_titles) do
  @doc = "Create track title file."

  ensurable

  newparam(:name) do
    desc "Mandaorty paramteter name ."
  end
  newproperty(:tracks) do
    desc "an arrary of strings"
  end

end

这是我的提供程序代码:(modules/hello_world/lib/puppet/provider/track_titles.rb)

Puppet::Type.type(:track_titles).provide(:foo) do
    desc "contrived example."

    def create
        filename = @resource[:name]
        tracks.each do |t|
            system ( "echo #{t} >> #{filename}" )
        end
    end

    def destroy
        File.unlink(@resource[:name])
    end

    def exists?
        File.exists?(@resource[:name]))
    end
end

这是我使用上述内容的 puppet 模块:(modules/hello_world/manifests/init.pp)

class hello_world (
        $msg = 'Hello World',
        $track_titles = ['one','two','three'],
) {
#       notify { $msg: }
        track_titles { '/tmp/track-titles.txt':
                tracks => $track_titles,
        }
}

我这样执行这段代码:

$ puppet apply \
> --modulepath=/home/red/PUPPET/modules \
> --hiera_config=/home/red/PUPPET/hiera.yaml \
> -e 'include hello_world'

这是我得到的输出:

Notice: Compiled catalog for localhost in environment production in 0.06 seconds
Error: /Stage[main]/Hello_world/Track_titles[/tmp/track-titles.txt]: Could not evaluate: No ability to determine if track_titles exists
Notice: Finished catalog run in 0.82 seconds

我做错了什么。还有一些我不明白的提供程序代码:

Puppet::Type.type(:track_titles).provide(:ruby) do

这个 .provide(:ruby) 是什么意思?

请帮忙:)

【问题讨论】:

  • 您可能不希望 ruby​​ 成为该类型的提供者。另外,我认为@resource[:name] 不会在您的提供程序代码中正确解析。要从概念上了解提供者是什么,请在此处查看 Peter 的精彩回答:stackoverflow.com/questions/41781030/…。另外,请查看:garylarizza.com/blog/2013/11/25/fun-with-providers。注意 Gary 的文章适用于 Puppet 3,但仍然相关。
  • 类型属性对应于资源的持久、托管状态是属性,而不是参数。看起来track_titles 应该属于这一类。
  • 谢谢约翰。我现在使用“newproperty”而不是“newparam”,但我得到了相同的结果。

标签: ruby puppet


【解决方案1】:

好的。这是我做错了什么。一方面,我没有仔细阅读文档 (https://docs.puppet.com/puppet/4.10/custom_types.html#properties-and-parameters)。它说:

提供程序文件应位于lib/puppet/provider/<TYPE NAME>/<PROVIDER NAME>.rb

了解这一点有助于弄清楚.provide(:thing) 的含义。 :thing<PROVIDER NAME> 是同一个东西,需要匹配。所以这是我更新的工作代码:

这是 Puppet 类:

$ cat modules/hello_world/manifests/init.pp
class hello_world (
        $track_titles = ['one','two','three'],
) {
        track_titles { '/tmp/track-titles.txt':
                tracks => $track_titles,
                ensure => present,
        }
}

这是类型定义:

$ cat modules/hello_world/lib/puppet/type/track_titles.rb
# blah blah blah
Puppet::Type.newtype(:track_titles) do
  @doc = "Create track title file."

  ensurable

  newparam(:name) do
    desc "Mandaorty paramteter name ."
  end
  newparam(:tracks) do
    desc "an arrary of strings"
  end
end

这里是提供者代码。 这是我将文件放在错误目录中的代码,这就是为什么我收到了 Could not evaluate: No ability to determine if track_titles exists 错误。

$ cat modules/hello_world/lib/puppet/provider/track_titles/track_titles.rb
Puppet::Type.type(:track_titles).provide(:track_titles) do
    desc "Contrived example"

    def create
        filename = @resource[:name]
        tracks = @resource[:tracks]
        tracks.each do |t|
            system ( "echo #{t} >> #{filename}" )
        end
    end

    def destroy
        File.unlink(@resource[:name])
    end

    def exists?
        File.exists?(@resource[:name])
    end
end

现在执行成功了:

$ puppet apply \
> --modulepath=/home/red/PUPPET/modules \
> --hiera_config=/home/red/PUPPET/hiera.yaml \
> -e 'include hello_world'

Notice: Compiled catalog for localhost in environment production in 0.07 seconds
Notice: /Stage[main]/Hello_world/Track_titles[/tmp/track-titles.txt]/ensure: created
Notice: Finished catalog run in 0.85 seconds

$ cat /tmp/track-titles.txt
one
two
three

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-26
    • 1970-01-01
    • 2014-12-18
    • 1970-01-01
    • 1970-01-01
    • 2013-05-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多