Best way to do custom facts based on 3rd party data

40 views
Skip to first unread message

Kimo Rosenbaum

unread,
Oct 23, 2014, 1:43:02 AM10/23/14
to puppet...@googlegroups.com
Hello,

I'm using puppet enterprise 3 with hiera and roles/profiles. I'm trying to find a good way to handle some facts (namely role) being generated on the master and sourced based on the underlying hypervisor. I.e., if the node is running on EC2 then the role would be from a tag, for vSphere the role would come from either a tag or custom attribute, etc, and a fallback would be to just take the first part of the hostname minus any number (e.g., web1.colo.domain.com would be web).

I know I can use custom facts on the client itself but I'd like to keep it on the master so I don't have to worry about credentials on all nodes, nodes not having access to the source (not having a route to the API, etc), nodes being able to "change" their role, etc.

The main use for this fact is in hiera:

hiera.yaml:
:hierarchy:
  - nodes/%{::clientcert}
  ...
  - roles/%{::role}
  ...
  common

Any suggestions?

Thanks
Kimo

Thomas Müller

unread,
Oct 23, 2014, 2:57:59 AM10/23/14
to puppet...@googlegroups.com
Hi Kimo

>
> I'm using puppet enterprise 3 with hiera and roles/profiles. I'm trying
> to find a good way to handle some facts (namely role) being generated on
> the master and sourced based on the underlying hypervisor. I.e., if the
> node is running on EC2 then the role would be from a tag, for vSphere
> the role would come from either a tag or custom attribute, etc, and a
> fallback would be to just take the first part of the hostname minus any
> number (e.g., web1.colo.domain.com would be web).


You can define $role in site.pp (manifests/site.pp) of your environment.
if defined outside the "node" definitions, it will be a top-scope
variable.


You can make decisions based on facts and set $role accordingly your
needs.

https://docs.puppetlabs.com/pe/latest/
puppet_assign_configurations.html#assigning-configuration-data-with-the-
site-manifest-sitepp



- Thomas

Kimo Rosenbaum

unread,
Oct 25, 2014, 2:11:58 AM10/25/14
to puppet...@googlegroups.com
Thanks. Here's a simplified version of what I ended up doing (for anyone searching for a similar thing):

In custom_functions/lib/puppet/parser/functions/determine_role.rb:

module Puppet::Parser::Functions
  newfunction(:determine_role, :type => :rvalue) do |args|
    # args[0] should be a fact name like clientcert or fqdn.
    hostname = lookupvar(args[0])
    matches = hostname.match(/^([^0-9.]+)[0-9.]/).captures
    role = matches[0]
    return role
  end
end

In manifests/site.pp

$role = determine_role('clientcert')

node default {
  hiera_include('classes')
}
Reply all
Reply to author
Forward
0 new messages