I've made some good progress, and am agonisingly close.

I have the below code which extracts the role into a variable (ugly, but it 
works).  I had kinda assumed that Facter.Add would work server-side, but 
I've discovered that it doesn't.  So now, my problem is simple how to make 
a string calculated in a function available beyond that function?  A fact 
would be great, a scoped variable would be fine, anything that exposes it 
in a way that hiera.yaml can get it's grubby little hands on it.

Any ideas?


Code - in dire need of betterification, but it will hopefully save other 
people a few hours of trial-and-error.

module Puppet::Parser::Functions
  newfunction(:get_role, :type => :rvalue) do |args|
    @roles=[]
    node=Puppet::Node.indirection.find( lookupvar('fqdn') )

    @role_lines=node.inspect.sub( /^.*?"role::/, "").split( /"role::/ )
    @role_lines.each do |rolestr|
      @roles.push rolestr.sub( /".*/, "")
    end
    @role=@roles.max_by(&:length).chomp

#    Facter.add( :tl_role ) do
#      setcode do
#        @role
#      end
#    end
#    $tl_role=@role

    return @role
  end
end


On Thursday, January 28, 2016 at 3:17:00 PM UTC, Gareth Humphries wrote:
>
> Hi All,
>
> I'm trying to add a new fact to puppet, which is the role the host has - 
> I'm using the popular roles/profiles methodology, so all nodes have exactly 
> one role::<something> class.  I want to get the <something> into a fact.
>
> I've managed to get it working easily enough, but either not automatically 
> or only on subsequent puppet runs (ie not the first).  This makes perfect 
> sense, given the work flow of collect-facts -> compile catalog -> 
> send-catalog-to-host.
>
> My next attempt is to write a server-side function that can analyse the 
> catalog after it's been built, extract the class names, find one that 
> matches /^role::/ and bob's your uncle.
>
> I'm having trouble getting started though - can anyone help me out with 
> how I might get access to the catalog?  Is there a variable I can lookup, 
> or a function or something?
>
> Is the catalog even available, or are functions run before compilation? 
>  Is there any way I can access a list of classes being applied?
>
>
> I don't want to require something be done by developers (for example, 
> putting the logic it in role.pp and require all roles to inherit that), as 
> that leaves too much room for mistakes.  I really want it to be 
> automatically done on all hosts, and available from the first run.
>
>
> I'm not married to parsing the catalog - the end goal here is just getting 
> the fact - any other ideas welcome.
>
>
>
> Thoughts?
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Puppet Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to puppet-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/puppet-users/c87a1f04-4335-4004-bc3b-06c3219be7a9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to