Skip to content

Instantly share code, notes, and snippets.

@binford2k
Created July 8, 2014 21:59
Show Gist options
  • Save binford2k/540451cc3dd961390352 to your computer and use it in GitHub Desktop.
Save binford2k/540451cc3dd961390352 to your computer and use it in GitHub Desktop.
require 'puppet/provider/parsedfile'
Puppet::Type.type(:host).provide(:classroom,
:parent => Puppet::Provider::ParsedFile,
:default_target => File.expand_path("~/etc/hosts"),
:filetype => :flat,
:record_type => :parsed,
) do
confine :exists => File.expand_path("~/etc")
confine :role => :student
defaultfor :osfamily => :redhat
defaultfor :role => :student
text_line :comment, :match => /^#/
text_line :blank, :match => /^\s*$/
record_line :parsed, :fields => %w{ip name host_aliases comment},
:optional => %w{host_aliases comment},
:match => /^(\S+)\s+(\S+)\s*(.*?)?(?:\s*#\s*(.*))?$/,
:post_parse => proc { |hash|
# An absent comment should match "comment => ''"
hash[:comment] = '' if hash[:comment].nil? or hash[:comment] == :absent
unless hash[:host_aliases].nil? or hash[:host_aliases] == :absent
hash[:host_aliases].gsub!(/\s+/,' ') # Change delimiter
end
},
:to_line => proc { |hash|
[:ip, :name].each do |n|
raise ArgumentError, "#{n} is a required attribute for hosts" unless hash[n] and hash[n] != :absent
end
str = "#{hash[:ip]}\t#{hash[:name]}"
if hash.include? :host_aliases and !hash[:host_aliases].nil? and hash[:host_aliases] != :absent
str += "\t#{hash[:host_aliases]}"
end
if hash.include? :comment and !hash[:comment].empty?
str += "\t# #{hash[:comment]}"
end
str
}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment