N way to get the top level classes included in a puppet node

puppet

Is there an easy way to return the top level classes applied to a node. By this I mean the classes included in a node definition or ENC equivalent For example with the sample node below, I'd like to return 'return::me' and 'return::me2' for somehost, but not 'dontreturn::me'

class return::me {
   include dontreturn::me
}
node "somehost" {
   include return::me
   include return::me2
}

I see that /var/lib/puppet/state/classes.txt includes the full list of classes applied to the node, but that includes any additional classes included by to the top level classes.

Is there some way to get at this data with a puppet faces command ? I suspect 'puppet nodes' might be useful, but I haven't been able to get it return what I what.

Best Answer

file { '/var/lib/puppet/state/classes.txt':
  content => inline_template("<%= Puppet::Node.indirection.find(fqdn).classes.join('\n') + '\n' %>"),
}

Works at least with recent 2.6.x and with 2.7.x (haven't tried earlier versions).

Given this uses internal Puppet APIs, I can't make any guarantees about this trick's longevity. Unfortunately there's nothing better anyway.

We used to have a puppet 2.6 trick using Puppet::Node.find(fqdn).classes instead, but it broke with 2.7.