Is this a Facter 4 bug? (using custom facts in other custom facts.)

44 views
Skip to first unread message

Mark de Vries

unread,
Jan 20, 2022, 1:09:34 PM1/20/22
to Puppet Users
Upgrade from facter 3.11.14 to 4.2.5 (puppet-agent 5 to 7) broke some of our custom facts, that use other (custom) facts.
Managed to find the smallest examples of two custom facts that reproduce the problem. 

First fact, that uses a core fact:

Facter.add(:my_fact) do
  hostname=Facter.value(:networking)['hostname']
  setcode do
    hostname
  end
end


Second fact, that uses the first fact:

my_fact = Facter.value(:my_fact)
Facter.add(:my_new_fact) do
  setcode do
    my_fact
  end
end


The first fact works, it's the second one that stops working with puppet7/facter4.

puppet5/facter3:
amvdi-it133:~# facter -p my_fact my_new_fact
my_fact => amvdi-it133
my_new_fact => amvdi-it133


puppet7/facter4:
mvdi-it133:~# facter -p my_fact my_new_fact
my_fact => amvdi-it133
my_new_fact =>

Seems to have to do with calling Facter.vaiue in the first fact.
This still does not work:

Facter.add(:my_fact) do
  unused=Facter.value(:networking)['hostname']
  hostname="testing"
  setcode do
  ...


But this does:

Facter.add(:my_fact) do
  hostname="testing"
  setcode do
  ...

# facter -p my_fact my_new_fact
my_fact => testing
my_new_fact => testing

Also found that it works if I change the second fact to do the Facter.value inside the setcode block:

Facter.add(:my_new_fact) do
  setcode do
    my_fact = Facter.value(:my_fact)
    my_fact
  end
end


Learning this I went back to the first fact and changed it to do the Facter.value inside the setcode block too like this:

Facter.add(:my_fact) do
  setcode do
    hostname=Facter.value(:networking)['hostname']
    hostname
  end
end


That also makes it work regardless of where I do Facter.value in the second fact.

I'm at a loss as to what the fact is going on here. Is it a bug? Or is there a reasonable explanation for it? (I'm a complete Ruby noob, )

Regards,
Mark.

KevinR

unread,
Jan 21, 2022, 6:48:14 AM1/21/22
to Puppet Users
The cause for your issues is that there's code before the setcode do line, which will cause issues under Facter 4.
Having code before setcode do was a bad coding practice but didn't break things in Facter 3. In Facter 4, it does break things.

If you move the hostname=Facter.value(:networking)['hostname'] statement to below setcode do, everything should be fine.

Kind regards,
Kevin

Mark de Vries

unread,
Jan 25, 2022, 2:28:07 AM1/25/22
to Puppet Users
Hi Keven, thank fou or clearing that up.

On Friday, January 21, 2022 at 12:48:14 PM UTC+1 KevinR wrote:
The cause for your issues is that there's code before the setcode do line, which will cause issues under Facter 4.
Having code before setcode do was a bad coding practice but didn't break things in Facter 3. In Facter 4, it does break things. 

Is there documentation I can/should read about this?

The puppet documentation about custom facts is very shallow, mostly by-example only.
Then there is the (auto generated?) documentation for the gem itself, which probably does a good job of documenting all the parts, but does not go into how to properly use them together.

Where could I have learned that, and why, code should be inside the setcode block?

Regards,
Mark.
 

KevinR

unread,
Jan 25, 2022, 7:21:21 AM1/25/22
to Puppet Users
Hi Mark,

you can find that documentation & guidance here: https://puppet.com/docs/puppet/7/fact_overview.html

Kind regards,
Kevin
Reply all
Reply to author
Forward
0 new messages