5

How would you use facter and puppet to determine if the OS is running CentOS 6.x or CentOS 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
1
  • 4
    Run facter with no arguments to see all the available facts. Aug 5, 2013 at 23:32

3 Answers 3

12

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.

7
  • 1
    Good info! Looks like operatingsystemmajrelease is only part of the newer versions of facter.
    – spuder
    Aug 5, 2013 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, 2013 at 22:57
  • // , Sadly, no, Teftin. $ facter operatingsystemmajrelease returns nothing on CEntOS 6.5 with Facter version 1.6.13. Dec 3, 2015 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, 2017 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, 2017 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" {
}
5
  • 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, 2013 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, 2013 at 23:28
  • When you anchor the regex, it's not necessary to end it with .*. In other words /^6.*/ is equivalent to /^6/. Aug 6, 2013 at 0:55
  • Without the .*, this would erroneously match 60.01, 60.02, 61.01, 62.02. Keep it.
    – gunwin
    Feb 12, 2015 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\..* Sep 8, 2015 at 7:06
2

With Puppet 5.5+ you should use a part of the compound os fact as operatingsystemmajrelease fact is a legacy fact now.

An example value of this fact:

{
    "name": "CentOS",
    "family": "RedHat",
    "release": {
        "full": "7.2.1511",
        "major": "7",
        "minor": "2"
    },
    "selinux": {
        "enabled": false
    },
    "hardware": "x86_64",
    "architecture": "x86_64"
}

Also please note that its value is a string, not an integer so remember about this when making comparisons:

case $facts['os']['release']['major'] {
    "6": {
        // do 6.x stuff
    }
    "5": {
        // do 5.x stuff
    }
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .