Book Image

Puppet Cookbook - Third Edition - Third Edition

Book Image

Puppet Cookbook - Third Edition - Third Edition

Overview of this book

This book is for anyone who builds and administers servers, especially in a web operations context. It requires some experience of Linux systems administration, including familiarity with the command line, file system, and text editing. No programming experience is required.
Table of Contents (17 chapters)
Puppet Cookbook Third Edition
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Comparing package versions


Package version numbers are odd things. They look like decimal numbers, but they're not: a version number is often in the form of 2.6.4, for example. If you need to compare one version number with another, you can't do a straightforward string comparison: 2.6.4 would be interpreted as greater than 2.6.12. And a numeric comparison won't work because they're not valid numbers.

Puppet's versioncmp function comes to the rescue. If you pass two things that look like version numbers, it will compare them and return a value indicating which is greater:

versioncmp( A, B )

returns:

  • 0 if A and B are equal

  • Greater than 1 if A is higher than B

  • Less than 0 if A is less than B

How to do it…

Here's an example using the versioncmp function:

  1. Modify your site.pp file as follows:

    node 'cookbook' {
      $app_version = '1.2.2'
      $min_version = '1.2.10'
    	
      if versioncmp($app_version, $min_version) >= 0 {
        notify { 'Version OK': }
      } else {
        notify { 'Upgrade needed': }
      }
    }
  2. Run Puppet...