木偶:确保按特定顺序排列

时间:2018-07-10 15:32:58

标签: ruby rubygems puppet

我是puppet的新手,需要为附加的代码定义新参数。 我想选择向附加的代码中添加订单行。

有人可以帮我举一些例子吗?

module Puppet
        newtype(:ensureline) do
                @doc = "ensureline doc"
                newparam(:name) do
                        desc "The name of the resource"
                end
                newparam(:file) do
                        desc "file to edit"
                end
                newparam(:line) do
                        desc "line to ensure"
                end
                newproperty(:ensure) do
                        desc 'Whether the line present or not?'
                        defaultto :present
                        def retrieve
                                File.readlines(resource[:file]).map {
                                        |line|
                                        line.chomp
                                }.include?(resource[:line]) ? :present : :absent
                        end
                        newvalue :absent do
                                # This block of code will make the :line absent in :file
                                data = File.readlines(resource[:file])
                                data.map! {
                                        |line|
                                        line.chomp
                                }.delete(resource[:line])
                                File.open(resource[:file], 'w') {
                                        |fd|
                                        fd.puts data
                                }
                        end
                        newvalue :present do
                                # This block of code will make the :line present in :file
                                File.open(resource[:file], 'a') {
                                        |fd|
                                        fd.puts resource[:line] + "\n"
                                }
                       end
                end
        end
end

谢谢

0 个答案:

没有答案