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
spuder
  • 1,695
  • 2
  • 25
  • 42

3 Answers3

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.

Tombart
  • 2,013
  • 3
  • 27
  • 47
Teftin
  • 1,931
  • 17
  • 9
  • 1
    Good info! Looks like operatingsystemmajrelease is only part of the newer versions of facter. – spuder Aug 05 '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 05 '13 at 22:57
  • // , Sadly, no, Teftin. `$ facter operatingsystemmajrelease` returns nothing on CEntOS 6.5 with Facter version 1.6.13. – Nathan Basanese Dec 03 '15 at 00: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
  • // , @Tombart, thanks for the response. I think perhaps it's a wrinkle of our version of Puppet (2.7), but `puppet apply -e "notify { $::operatingsystemmajrelease : }"` gets me `/Stage[main]//Notify[undef]/message: defined 'message' as 'undef'` – Nathan Basanese Jan 13 '17 at 02:34
  • // , Same with `puppet apply -e "notify { $::os[release][major] : }"`. – Nathan Basanese Jan 13 '17 at 02:35
  • 1
    @NathanBasanese You need at least Facter 1.7 in order to have `operatingsystemmajrelease`, see [the docs for available facts](https://docs.puppet.com/facter/1.7/core_facts.html). 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" {
}
Martijn Heemels
  • 7,438
  • 6
  • 39
  • 62
Zypher
  • 36,995
  • 5
  • 52
  • 95
  • 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 05 '13 at 23:25
  • It's not puppet specific but part of regex. It's a regex anchor (http://www.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 05 '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 06 '13 at 00:55
  • Without the `.*`, this would erroneously match 60.01, 60.02, 61.01, 62.02. Keep it. – gunwin Feb 12 '15 at 00: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 08 '15 at 07:06
1

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
    }
}
Greg Dubicki
  • 1,191
  • 1
  • 14
  • 30