Puppet treats the text supplied between the forward slashes as a regular expression, specifying the text to be matched. If the match succeeds, the if expression will be true and so the code between the first set of curly braces will be executed. In this example, we used a regular expression because different distributions have different ideas on what to call 64 bit; some use amd64, while others use x86_64. The only thing we can count on is the presence of the number 64 within the fact. Some facts that have version numbers in them are treated as strings to Puppet. For instance, $::facterversion. On my test system, this is 3.9.3, but when I try to compare that with 3, Puppet fails to make the following comparison:
if $::facterversion > 3 {
notify {"Facter version 3": }
}
Which produces the following output when run with puppet apply:
t@cookbook:~$ puppet apply version.pp
Error: Evaluation Error: Comparison of: String > Integer, is not possible. Caused by 'A String is not comparable to a non String'. at /home/vagrant/version.pp:1:21 on node cookbook.example.com
We could make the comparison with =~ but that would match a 3 in any position in the version string. Puppet provides a function to compare versions, versioncmp, as shown in this example:
if versioncmp($::facterversion,'3') > 0 {
notify {"Facter version 3": }
}
Which now produces the desired result:
t@cookbook:~$ puppet apply version2.pp
Notice: Compiled catalog for cookbook.strangled.net in environment production in 0.01 seconds
Notice: Facter version 3
The versioncmp function returns -1 if the first parameter is a lower version than the second, 0 if the two parameters are equal, or 1 if the second parameter is lower than the first.
If you wanted instead to do something if the text does not match, use !~ rather than =~:
if $::kernel !~ /Linux/ {
notify { 'Not Linux, could be Windows, MacOS X, AIX, or ?': }
}