Skip to content

Instantly share code, notes, and snippets.

@natemccurdy
Last active May 4, 2016 17:29
Show Gist options
  • Save natemccurdy/60094f0e7fa3a01478996b83d735330a to your computer and use it in GitHub Desktop.
Save natemccurdy/60094f0e7fa3a01478996b83d735330a to your computer and use it in GitHub Desktop.
application fact
# This custom fact will figure out which application
# a node belongs to by looking at the node's hostname
# and comparing that to a known list of applications.
#
# This fact relies on a naming convention where the
# first three letters of a node's hostname signify
# what application that node belongs to.
#
Facter.add(:application) do
setcode do
# This map is the list of applications and their
# possible abbreviations that could be in a hostname.
application_map = {
'webserver' => [ 'web', 'www', 'iis' ],
'remote' => [ 'rem', 'rdp' ],
'gitlab' => [ 'git' ],
'puppet' => [ 'pup' ],
}
# Get the first 3 letters of the node's hostname, and
# make sure it's lowercase.
hostname = Facter.value(:hostname).downcase
hostname_first_three = hostname[0..2]
# Create an empty variable to store the final result.
application = nil
# Iterate through each element of the application_map
# and see if the abbreviations match the first three
# letters of a host's name. If it does match, save
# the application name and stop searching for matches.
application_map.each do | app_name, abbreviations |
if abbreviations.include? hostname_first_three
application = app_name
break
end
end
# Return the application name.
# This will be nil (undef in Puppet) if a match isn't found.
application
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment