Moment of duhh.... Trying to wrap my head around some condition statements.

75 views
Skip to first unread message

Scott Walker

unread,
Sep 26, 2016, 6:23:17 PM9/26/16
to Puppet Users
I'm relatively new to puppet (came from chef world).

I'm trying to figure out a way to cleanly do the following...

On machines which /usr/sbin/dmidecode | grep Z640 (this is how we are sure the machine is Z640 and not some other type of machine) return true I want to do the following:

file { '/etc/default/grub':
    path    => '/etc/default/grub'
    ensure => present,
    mode    => '0644',
    owner    => 'root',
    group   => 'root',
    notify => Exec['grub-update'],
    source => 'puppet:///modules/z640/grub'
}

exec { 'grub-update':
  refreshonly => true,
  command => '/sbin/grub2-mkconfig -o /boot/grub2/grub.cfg',
    }

Otherwise I don't want to do anything. (This is to help fix an NVME issue I am having on 60+ workstation out of 700 in the studio.

I just can't wrap my head around a clean way to achieve this goal.

I know this is probably a really simple task I just need some direction....

Nick Miller

unread,
Sep 26, 2016, 6:39:45 PM9/26/16
to puppet...@googlegroups.com
Hey Scott,

You probably want to reference a Fact, whether custom or not. Check `puppet facts find $(hostname)` to see what facts are available on your systems. If you don't find on you like, you may have to write one following this guide. It should be fairly easy to make a dmidecode fact if there isn't one already.

Good luck,
Nick

--
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/024553d2-1872-4eab-9bd5-8280c92c07ec%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--

OnyxPoint-logo-symbol-primary.png

Nicholas Miller
Consultant | Onyx Point, Inc.

7050 Hi Tech Drive, Suite 102

Hanover, MD. 21076
e: nick....@onyxpoint.com
w: 443-655-3675

copmany.pngcareers.pngproduct.pngmeetups.pngblog.png

Scott Walker

unread,
Sep 27, 2016, 11:38:00 AM9/27/16
to Puppet Users
Ahh perfect there is.. Now onto the next battle.. figuring out how to use it lol.
To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users...@googlegroups.com.

Christopher Wood

unread,
Sep 27, 2016, 1:39:30 PM9/27/16
to puppet...@googlegroups.com
This is one of the first custom facts I wrote, you would likely want to improve on it based on the guide. However it has worked for us for a few years now. Enjoy?

This lives in inventory/lib/facter/hpsrl.rb.


if FileTest.exists?("/usr/sbin/dmidecode")

# Add remove things to query here
query = {
'HP ProLiant System/Rack Locator' => [
'Rack Name:',
'Enclosure Name:',
'Enclosure Model:',
'Enclosure Serial:',
'Enclosure Bays:',
'Server Bay:',
'Bays Filled:',
]
}

# Run dmidecode only once
output=%x{/usr/sbin/dmidecode 2>/dev/null}

query.each_pair do |key,v|
v.each do |value|
output.split("Handle").each do |line|
if line =~ /#{key}/ and line =~ /#{value} (\w.*)\n*./
result = $1
result = result.gsub(/ *$/, '').gsub(/^ */, '')
Facter.add(value.chomp(':').gsub(/ /, '_')) do
confine :kernel => :linux
setcode do
result
end
end
end
end
end
end
end



On Tue, Sep 27, 2016 at 08:38:00AM -0700, Scott Walker wrote:
> Ahh perfect there is.. Now onto the next battle.. figuring out how to use
> it lol.
>
> On Monday, 26 September 2016 18:39:45 UTC-4, Nick Miller wrote:
>
> Hey Scott,
>
> You probably want to reference a Fact, whether custom or not. Check
> `puppet facts find $(hostname)` to see what facts are available on your
> systems. If you don't find on you like, you may have to write one
> following [1]this guide. It should be fairly easy to make a dmidecode
> an email to [3]puppet-users...@googlegroups.com.
> To view this discussion on the web visit
> [4]https://groups.google.com/d/msgid/puppet-users/024553d2-1872-4eab-9bd5-8280c92c07ec%40googlegroups.com.
> For more options, visit [5]https://groups.google.com/d/optout.
>
> --
>
> Nicholas Miller
> Consultant | Onyx Point, Inc.
>
> [6]OnyxPoint-logo-symbol-primary.png 7050 Hi Tech Drive, Suite 102
>
> Hanover, MD. 21076
> e: [7]nick....@onyxpoint.com
> w: 443-655-3675
> [8]copmany.png[9]careers.png[10]product.png[11]meetups.png[12]blog.png
>
> --
> 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 [13]puppet-users...@googlegroups.com.
> To view this discussion on the web visit
> [14]https://groups.google.com/d/msgid/puppet-users/3c190210-fa30-48f2-8d18-4835c54cfe53%40googlegroups.com.
> For more options, visit [15]https://groups.google.com/d/optout.
>
> References
>
> Visible links
> 1. https://docs.puppet.com/facter/3.4/custom_facts.html
> 2. javascript:
> 3. javascript:
> 4. https://groups.google.com/d/msgid/puppet-users/024553d2-1872-4eab-9bd5-8280c92c07ec%40googlegroups.com?utm_medium=email&utm_source=footer
> 5. https://groups.google.com/d/optout
> 7. javascript:
> 13. mailto:puppet-users...@googlegroups.com
> 14. https://groups.google.com/d/msgid/puppet-users/3c190210-fa30-48f2-8d18-4835c54cfe53%40googlegroups.com?utm_medium=email&utm_source=footer
> 15. https://groups.google.com/d/optout

Scott Walker

unread,
Sep 27, 2016, 1:49:08 PM9/27/16
to Puppet Users
Very nice! Thanks!

John Gelnaw

unread,
Sep 27, 2016, 1:58:59 PM9/27/16
to Puppet Users

if ($productname =~ /Z640/)  {

  file { ... }
  exec { ... } 

}

'productname' is a fact which usually represents your system model.

Scott Walker

unread,
Sep 27, 2016, 3:36:42 PM9/27/16
to Puppet Users
This is what I came up with.

# Turn on fsck.auto on Z640 for NVME drives.
class z640 {

if $::productname == 'HP Z640 Workstation' {

notice('This is a Z640 workstation')

file { '/etc/default/grub':
  ensure => present,
  path   => '/etc/default/grub',

  mode   => '0644',
  owner  => 'root',
  group  => 'root',
  notify => Exec['grub-update'],
  source => 'puppet:///modules/z640/grub'
  }

exec { 'forcefsck':
  command => '/bin/echo "-y" >/forcefsck',
  onlyif  => '/usr/bin/test ! -f /forcefsck'

}

exec { 'grub-update':
  refreshonly => true,
  command     => '/sbin/grub2-mkconfig -o /boot/grub2/grub.cfg',
  }


  }

}


markber...@gmail.com

unread,
Sep 28, 2016, 8:57:35 AM9/28/16
to puppet...@googlegroups.com
In the message dated: Tue, 27 Sep 2016 13:38:16 -0400,
The pithy ruminations from Christopher Wood on
<Re: [Puppet Users] Moment of duhh.... Trying to wrap my head around some condi
tion statements.> were:
=> This is one of the first custom facts I wrote, you would likely want to improve on it based on the guide. However it has worked for us for a few years now. Enjoy?
=>
=> This lives in inventory/lib/facter/hpsrl.rb.
=>
=>
=> if FileTest.exists?("/usr/sbin/dmidecode")
=>
=> # Add remove things to query here
=> query = {
=> 'HP ProLiant System/Rack Locator' => [
=> 'Rack Name:',
=> 'Enclosure Name:',
=> 'Enclosure Model:',
=> 'Enclosure Serial:',
=> 'Enclosure Bays:',
=> 'Server Bay:',
=> 'Bays Filled:',
=> ]
=> }
=>
=> # Run dmidecode only once
=> output=%x{/usr/sbin/dmidecode 2>/dev/null}
=>
=> query.each_pair do |key,v|
=> v.each do |value|
=> output.split("Handle").each do |line|
=> if line =~ /#{key}/ and line =~ /#{value} (\w.*)\n*./
=> result = $1
=> result = result.gsub(/ *$/, '').gsub(/^ */, '')
=> Facter.add(value.chomp(':').gsub(/ /, '_')) do
=> confine :kernel => :linux
=> setcode do
=> result
=> end


I don't really (or, "really don't") do Ruby, but it looks to me like
that sets the facts using the original case of the data returned from
dmidecode. Maybe this is OK when creating facts by calling Facter.add(),
and maybe this is OK for certain versions of puppet.

I'm using puppet 3.8, and setting facts via a shell script that returns
strings (see below for a heavily abridged example). In this case, the
variable names _must_ begin with a lower-case letter.

https://tickets.puppetlabs.com/browse/FACT-777

---------------------------------------------
#! /bin/bash
#
# return hardware "facts" for use by facter & puppet
#
# since these facts are specific to particular modules (ie. setting IPMI parameters),
# we choose to store the script that generates the facts with the module that consumes
# those facts, as in:
#
# $PUPPETHOME/environments/computenode/modules/ipmi/facts.d/ipmi_facts

which dmidecode 1> /dev/null 2>&1
if [ $? != 0 ] ; then
echo dmidecode=MISSING
exit
else
echo dmidecode=INSTALLED
fi

# Does this machine have an IPMI device?
dmidecode | grep -q ^IPMI
if [ $? = 0 ] ; then
if [ -c /dev/ipmi0 ] ; then
ipmi=true
fi
fi
---------------------------------------------


=> end
=> end
=> end
=> end
=> end
=> end
=>
=>

--
Mark

Christopher Wood

unread,
Sep 28, 2016, 9:42:19 AM9/28/16
to puppet...@googlegroups.com
In 3.4 to 3.8 I did get lower-cased fact names from the ruby custom fact there just like FACT-777 points out. I've never tried fact names with upper-cased characters so never ran into this.
> --
> 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.
> To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/29256-1475002757.850308%40OpxQ.RGhU.3HbK.
Reply all
Reply to author
Forward
0 new messages