writing custom fact -> return value not as expected

115 views
Skip to first unread message

Denny

unread,
Jan 10, 2017, 11:44:23 AM1/10/17
to Puppet Users
Hi there,

probably a pretty easy to answer question.

I want to try out adding custom facts. My first custom fact should be "lastrebootdate"

My code looks like this:

Facter.add(:lastrebootdate) do
  setcode do
    Facter::Util::Resolution.exec("/usr/bin/who -b |awk '{print $3}'")
  end
end

Running the command on the system returns "2017-01-30"

Deploying my fact on a puppet node and running the puppet agent returns "Jan".

$ puppet facts |grep lastrebootdate
    "lastrebootdate": "Jan",

What did I miss?

Thank you,

Denny

Denny

unread,
Jan 10, 2017, 11:47:36 AM1/10/17
to Puppet Users
PS: I'm running facter 3.5.0 with puppet 4.8.1 on CentOS 7
Message has been deleted

Denny

unread,
Jan 10, 2017, 12:24:45 PM1/10/17
to Puppet Users
Tried out another customfact "lastyumupdate" which looks like:

Facter.add(:lastyumupdate) do
  setcode do
    Facter::Util::Resolution.exec("yum history |grep -E '^.*(Update| U).*$' |head -n 1 |awk '{print $6}'")
  end
end

This one returns on command line "2017-01-10" AND sets the fact correct

$ puppet facts |grep last
    "lastrebootdate": "Jan",
    "lastyumupdate": "2017-01-10",


Any help is appreciated :)


Denny

Rob Nelson

unread,
Jan 10, 2017, 3:14:58 PM1/10/17
to Puppet Users
At a guess, dollar signs inside double quotes interpolate, so it's extremely possible that somewhere earlier in the ruby run, $3 matched "Jan" somewhere and that was reused in your awk command. In the latter usage there's probably no $6 (that's a lot of matches!) or it amazingly has the value '$6'. I would definitely be more careful about escaping any dollars inside of double quoted strings that are passed to exec(), system(), or similar functions, as escaping that can be a nightmare when the stars align during your design but not weeks later during your usage.

Stefan Schulte

unread,
Jan 10, 2017, 4:30:43 PM1/10/17
to puppet...@googlegroups.com
Hi Denny,

IIRC facter will internally run commands with a C locale to get reliable
command output on different locale settings [1]

% LC_ALL=de_DE.utf8 /usr/bin/who -b |awk '{print $3}'
19:34
% LC_ALL=en_US.utf8 /usr/bin/who -b |awk '{print $3}'
2017-01-10
% LC_ALL=C /usr/bin/who -b |awk '{print $3}'
Jan

[1] https://github.com/puppetlabs/facter/pull/696
> --
> 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...@googlegroups.com
> <mailto:puppet-users...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/puppet-users/f91eae2b-b980-4dd5-a544-2fc0a2f8e9ef%40googlegroups.com
> <https://groups.google.com/d/msgid/puppet-users/f91eae2b-b980-4dd5-a544-2fc0a2f8e9ef%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.

signature.asc

Stefan Schulte

unread,
Jan 10, 2017, 4:47:54 PM1/10/17
to puppet...@googlegroups.com
Hey Rob,

variable interpolation in strings in ruby is actually done with
#{some_var}, so the following snippet

#!/usr/bin/ruby

"Hello World".match(/Hello (.*)/)

puts $1
puts "$1"
puts "#{$1}

actually returns

World
$1
World

As you can see "$1" does not interpolate to an earlier match.
> --
> 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...@googlegroups.com
> <mailto:puppet-users...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/puppet-users/1fe52550-e656-415a-9197-a692d397c8bc%40googlegroups.com
> <https://groups.google.com/d/msgid/puppet-users/1fe52550-e656-415a-9197-a692d397c8bc%40googlegroups.com?utm_medium=email&utm_source=footer>.

Rob Nelson

unread,
Jan 10, 2017, 5:43:08 PM1/10/17
to puppet...@googlegroups.com
Thanks for the correction. Definitely way off base on that, my apologies for the erroneous claims!

> To view this discussion on the web visit
> For more options, visit https://groups.google.com/d/optout.

--
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/ccb700cd-6bf1-14dc-84cf-9b75d7181eca%40posteo.de.

Dirk Heinrichs

unread,
Jan 11, 2017, 2:07:06 AM1/11/17
to puppet...@googlegroups.com
Am 10.01.2017 um 22:29 schrieb Stefan Schulte:

% LC_ALL=de_DE.utf8 /usr/bin/who -b |awk '{print $3}'
19:34
% LC_ALL=en_US.utf8 /usr/bin/who -b |awk '{print $3}'
2017-01-10

Interesting. On my system, there's no difference between them (left out the awk part for clarity):

% LC_ALL=en_US.UTF-8 who -b
         system boot  2017-01-02 11:10
% LC_ALL=de_DE.UTF-8 who -b
         system boot  2017-01-02 11:10


% LC_ALL=C /usr/bin/who -b |awk '{print $3}'
Jan

Only here:

% LC_ALL=C who -b
         system boot  Jan  2 11:10

Bye...

    Dirk
--
Dirk Heinrichs
Senior Systems Engineer, Delivery Pipeline
OpenTextTM Discovery | Recommind
Email: dirk.he...@recommind.com
Website: www.recommind.de

Recommind GmbH, Von-Liebig-Straße 1, 53359 Rheinbach

Vertretungsberechtigte Geschäftsführer John Marshall Doolittle, Gordon Davies, Roger Illing, Registergericht Amtsgericht Bonn, Registernummer HRB 10646

This e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error) please notify the sender immediately and destroy this e-mail. Any unauthorized copying, disclosure or distribution of the material in this e-mail is strictly forbidden

Diese E-Mail enthält vertrauliche und/oder rechtlich geschützte Informationen. Wenn Sie nicht der richtige Adressat sind oder diese E-Mail irrtümlich erhalten haben, informieren Sie bitte sofort den Absender und vernichten Sie diese Mail. Das unerlaubte Kopieren sowie die unbefugte Weitergabe dieser Mail sind nicht gestattet.

Denny

unread,
Jan 11, 2017, 3:26:40 AM1/11/17
to Puppet Users, dirk.he...@recommind.com
Thank you guys for the clarification!

Stupid question #2:

How do I get facter use LC_ALL=en_US.UTF-8 instead of LC_ALL=C?

A simple add to my Facter::Util::Resolution.exec("...") did not work, of course.


Denny

Thomas Müller

unread,
Jan 13, 2017, 6:27:47 AM1/13/17
to Puppet Users


Am Dienstag, 10. Januar 2017 17:44:23 UTC+1 schrieb Denny:
Hi there,

probably a pretty easy to answer question.

I want to try out adding custom facts. My first custom fact should be "lastrebootdate"

My code looks like this:

Facter.add(:lastrebootdate) do
  setcode do
    Facter::Util::Resolution.exec("/usr/bin/who -b |awk '{print $3}'")
  end
end

Running the command on the system returns "2017-01-30"



just a random thought: instead of calling who you could take the already existing uptime fact and just calculate the date with ruby time/date functions.

- Thomas
 

Rob Nelson

unread,
Jan 13, 2017, 7:32:19 AM1/13/17
to puppet...@googlegroups.com
There's also `last reboot -1`, add your favorite timestamp format with -T :)

--
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+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages