11
Puppet::Type.type(:host).provide(
13
:parent => Puppet::Provider::ParsedFile,
14
:default_target => hosts,
11
Puppet::Type.type(:host).provide(:parsed,:parent => Puppet::Provider::ParsedFile,
12
:default_target => hosts,:filetype => :flat) do
18
13
confine :exists => hosts
20
15
text_line :comment, :match => /^#/
21
16
text_line :blank, :match => /^\s*$/
23
record_line :parsed, :fields => %w{ip name host_aliases},
24
:optional => %w{host_aliases},
25
:rts => true do |line|
27
if line.sub!(/^(\S+)\s+(\S+)\s*/, '')
32
hash[:host_aliases] = []
35
line.sub!(/^([^#]+)\s*/) do |value|
37
unless aliases =~ /^\s*$/
38
hash[:host_aliases] = aliases.split(/\s+/)
45
raise Puppet::Error, "Could not match '#{line}'"
48
hash[:host_aliases] = [] if hash[:host_aliases] == ""
53
# Convert the current object into a host-style string.
54
def self.to_line(hash)
55
return super unless hash[:record_type] == :parsed
56
[:ip, :name].each do |n|
57
raise ArgumentError, "#{n} is a required attribute for hosts" unless hash[n] and hash[n] != :absent
60
str = "#{hash[:ip]}\t#{hash[:name]}"
62
if hash.include? :host_aliases and !hash[:host_aliases].empty?
63
if hash[:host_aliases].is_a? Array
64
str += "\t#{hash[:host_aliases].join("\t")}"
66
raise ArgumentError, "Host aliases must be specified as an array"
18
record_line :parsed, :fields => %w{ip name host_aliases comment},
19
:optional => %w{host_aliases comment},
20
:match => /^(\S+)\s+(\S+)\s*(.*?)?(?:\s*#\s*(.*))?$/,
21
:post_parse => proc { |hash|
22
# An absent comment should match "comment => ''"
23
hash[:comment] = '' if hash[:comment].nil? or hash[:comment] == :absent
24
unless hash[:host_aliases].nil? or hash[:host_aliases] == :absent
25
hash[:host_aliases].gsub!(/\s+/,' ') # Change delimiter
28
:to_line => proc { |hash|
29
[:ip, :name].each do |n|
30
raise ArgumentError, "#{n} is a required attribute for hosts" unless hash[n] and hash[n] != :absent
32
str = "#{hash[:ip]}\t#{hash[:name]}"
33
if hash.include? :host_aliases and !hash[:host_aliases].nil? and hash[:host_aliases] != :absent
34
str += "\t#{hash[:host_aliases]}"
36
if hash.include? :comment and !hash[:comment].empty?
37
str += "\t# #{hash[:comment]}"