Sha256: 145038088fb2774281ef37d65f666607e8dd993ede909d59b060682f76db9817

Contents?: true

Size: 1.81 KB

Versions: 10

Compression:

Stored size: 1.81 KB

Contents

require 'puppet/provider/parsedfile'

hosts = nil
case Facter.value(:operatingsystem)
when "Solaris": hosts = "/etc/inet/hosts"
else
    hosts = "/etc/hosts"
end

Puppet::Type.type(:host).provide(:parsed,
    :parent => Puppet::Provider::ParsedFile,
    :default_target => hosts,
    :filetype => :flat
) do
    confine :exists => hosts

    text_line :comment, :match => /^#/ 
    text_line :blank, :match => /^\s*$/ 

    record_line :parsed, :fields => %w{ip name alias},
        :optional => %w{alias},
        :rts => true do |line|
        hash = {}
        if line.sub!(/^(\S+)\s+(\S+)\s*/, '')
            hash[:ip] = $1
            hash[:name] = $2

            unless line == ""
                line.sub!(/\s*/, '')
                line.sub!(/^([^#]+)\s*/) do |value|
                    aliases = $1
                    unless aliases =~ /^\s*$/
                        hash[:alias] = aliases.split(/\s+/)
                    end

                    ""
                end
            end
        else
            raise Puppet::Error, "Could not match '%s'" % line
        end

        if hash[:alias] == ""
            hash.delete(:alias)
        end

        return hash
    end

    # Convert the current object into a host-style string.
    def self.to_line(hash)
        return super unless hash[:record_type] == :parsed
        [:ip, :name].each do |n|
            unless hash[n] and hash[n] != :absent
                raise ArgumentError, "%s is a required attribute for hosts" % n
            end
        end

        str = "%s\t%s" % [hash[:ip], hash[:name]]

        if hash.include? :alias
            if hash[:alias].is_a? Array
                str += "\t%s" % hash[:alias].join("\t")
            else
                raise ArgumentError, "Aliases must be specified as an array"
            end
        end

        str
    end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
puppet-0.24.9 lib/puppet/provider/host/parsed.rb
puppet-0.24.0 lib/puppet/provider/host/parsed.rb
puppet-0.24.4 lib/puppet/provider/host/parsed.rb
puppet-0.24.3 lib/puppet/provider/host/parsed.rb
puppet-0.24.2 lib/puppet/provider/host/parsed.rb
puppet-0.24.1 lib/puppet/provider/host/parsed.rb
puppet-0.24.5 lib/puppet/provider/host/parsed.rb
puppet-0.24.7 lib/puppet/provider/host/parsed.rb
puppet-0.24.6 lib/puppet/provider/host/parsed.rb
puppet-0.24.8 lib/puppet/provider/host/parsed.rb