puppetfacter

node name within custom puppet fact?


How do I access the name of the puppet node (i.e., $trusted['certname'] in a manifest) within the context of a custom fact (i.e., lib/facter/my_custom_fact.rb).

I read this question/answer, and that filled me with hope, but alas, I see no reference to the node name in the list of available facts accessible via Facter.value(:sym).

Thank you.


Solution

  • I guess I'm explaining a lot you already know but I'll start at the beginning for context.

    1. At the beginning of the Puppet agent run, plugin sync runs, this pulls down and custom facts so this is when your my_custom_fact.rb will be pulled down to your node.
    2. Then Puppet gathers the facts so it gathers the regular facts and any custom facts that plugin sync pulled down, so this is when the my_custom_fact.rb will run, it then sends those facts up to the PE server. At this point you could test your fact by running facter -p my_custom_fact.
    3. Those facts become available along with the trusted facts that are baked into the target node certificate, these are only available on the Puppet server because it needs it's private key to decrypt them from the certificate (see https://puppet.com/docs/puppet/7/ssl_attributes_extensions.html for more info).
    4. Puppet uses those facts to compile the catalog.

    So you can see that the fact you want just isn't available to you at the time you're asking for it, you're asking for it at stage 2 but it's not decrypted from the node's certificate until stage 3.

    Once on the Puppet server though it becomes available as a node scope variable $::trusted['certname'], from here you could pass it into a manifest or a custom function, anything that runs during stage 4.

    I think the only way you'll be able to get the value into my_custom_fact.pp is to use one of the available facts from the target node when the facts are being run during step 2 and you should be able to do it by just adding the following line to my_custom_fact.pp;

    my_fqdn = Facter.value('fqdn')
    

    This is from https://puppet.com/docs/puppet/6/custom_facts.html#using_other_facts

    The networking.fqdn fact is a regular facter fact, you can check that any time on a system running facter networking.fqdn or its alias facter fqdn.