5

How would you use facter and puppet to determine if the OS is running Cent 6.x or Cent 5.x ?

facter operatingsystemrelease 
6.4

I only care about the major release (6)

I've thought about using awk, but there must be a better way that is more 'puppet manifest' friendly.

   #This works, but is ugly trying to use this in a puppet manifest

facter operatingsystemrelease |awk -F. '{print $1}'
6

Update:

It looks like the newer versions of facter have some additional information about major releases that isn't in my version. My initial provisioning needs to assume that facter is out of date.

facter --version
1.6.4
puppet --version
2.7.20

I've tried searching for any additional facts that might show the major release, with the following command

facter |grep 6
  • 4
    Run facter with no arguments to see all the available facts. – Michael Hampton Aug 5 '13 at 23:32
11

There is operatingsystemmajrelease

% facter operatingsystemmajrelease
6

If you have redhat-lsb-core package installed, facter will get as well the family of lsb-provided facts (which includes lsbmajdistrelease):

% facter |grep ^lsb
lsbdistcodename => Final
lsbdistdescription => CentOS release 6.4 (Final)
lsbdistid => CentOS
lsbdistrelease => 6.4
lsbmajdistrelease => 6
lsbrelease => :base-4.0-amd64:base-4.0-noarch:core-4.0-amd64:core-4.0-noarch

NOTE: You need at least Facter 1.7 in order have operatingsystemmajrelease. Core facts in Facter 1.6 are quite limited.

  • 1
    Good info! Looks like operatingsystemmajrelease is only part of the newer versions of facter. – spuder Aug 5 '13 at 22:51
  • 1
    Puppetlabs yum repos have up-to-date versions of facter for both CentOS 5 and 6. Afair lsb-derived facts work with old facter though. – Teftin Aug 5 '13 at 22:57
  • // , Sadly, no, Teftin. $ facter operatingsystemmajrelease returns nothing on CEntOS 6.5 with Facter version 1.6.13. – Nathan Basanese Dec 3 '15 at 0:23
  • 1
    @NathanBasanese try executing puppet apply -e "notify { $::operatingsystemmajrelease : }". With newer facter also hierarchical facts should work puppet apply -e "notify { $::os[release][major] : }" and the old ones are not displayed standard facter output. – Tombart Jan 12 '17 at 18:47
  • 1
    @NathanBasanese You need at least Facter 1.7 in order to have operatingsystemmajrelease, see the docs for available facts. For hierarchical facts Facter 3 and newer are needed. – Tombart Jan 13 '17 at 10:07
5

I'm guessing that you are trying to make some sort of decision based off of the install version.

You can use regexes in your logic.

So something like:

case $operatingsystemrelease {
    /^6.*/: {
        //do 6.x stuff
    }
    /^5.*/: {
        //do 5.x stuff
    }
}

or if if is more your style:

if $operatingsystemrelease =~ /^6.*/ {
    //do 6.x stuff
}
elsif $operatingsystemrelease =~ /^5.*/ {
   // do 5.x stuff
}

Remember that all factor facts are available in global scope variables to your manifests.

If you have a mixed environment you will probably want to wrap that in in something like:

if $operatingsystem == "CentOS" {
}
  • Thanks @Zypher, I'm not able to find anything in the puppet documentation about the ^ character. Could you explain why you put ^ in front of the digits? Google isn't offering much help. – spuder Aug 5 '13 at 23:25
  • It's not puppet specific but part of regex. It's a regex anchor (regular-expressions.info/anchors.html). Basically if you didn't do that it could possibly match 6.5 as v5 or 5.6 as v6 – Zypher Aug 5 '13 at 23:28
  • When you anchor the regex, it's not necessary to end it with .*. In other words /^6.*/ is equivalent to /^6/. – Dennis Williamson Aug 6 '13 at 0:55
  • Without the .*, this would erroneously match 60.01, 60.02, 61.01, 62.02. Keep it. – gunwin Feb 12 '15 at 0:41
  • 4
    @gunwin The .* will also match 60.01, etc... due to the period being a special regex char meaning "Match any single character" instead of a literal period. An extra escaped period should be added to get the result you describe, e.g. ^6\..* – Martijn Heemels Sep 8 '15 at 7:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.