无法弄清楚如何编写自定义类型

时间:2017-04-29 07:32:05

标签: ruby puppet

更新:

我正在努力教自己如何编写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)的全部内容?

请帮助:)

1 个答案:

答案 0 :(得分:0)

行。这就是我做错了。对于一个我没有阅读文档(https://docs.puppet.com/puppet/4.10/custom_types.html#properties-and-parameters)足够接近。它说:

提供商文件应位于- hosts: localhost gather_facts: false vars: file_vars: - {name: file1} - {name: file2} tasks: - name: Checking existing file name stat: path: ./{{ item.name }} with_items: "{{ file_vars }}" register: check_file_name - debug: msg: 'file name {{item.item.name}} not exists' with_items: "{{ check_file_name.results }}" when: item.stat.exists == False - name: Create file file: path: "./{{item.item.name}}" state: touch with_items: "{{ check_file_name.results }}" when: item.stat.exists == False

了解这有助于弄清楚lib/puppet/provider/<TYPE NAME>/<PROVIDER NAME>.rb的含义。 .provide(:thing):thing是相同的,需要匹配。所以这是我更新的工作代码:

这是Puppet类:

<PROVIDER NAME>

这是类型定义:

$ 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,
        }
}

这是提供商代码。 这是我将文件放在WRONG目录中的代码,这就是我收到 $ 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