Book Image

Puppet Cookbook - Third Edition - Third Edition

By : Thomas Uphill, John Arundel
Book Image

Puppet Cookbook - Third Edition - Third Edition

By: Thomas Uphill, John Arundel

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 (12 chapters)
11
Index

Creating custom facts

While Facter's built-in facts are useful, it's actually quite easy to add your own facts. For example, if you have machines in different data centers or hosting providers, you could add a custom fact for this so that Puppet can determine whether any local settings need to be applied (for example, local DNS servers or network routes).

How to do it...

Here's an example of a simple custom fact:

  1. Create the directory modules/facts/lib/facter and then create the file modules/facts/lib/facter/hello.rb with the following contents:
    Facter.add(:hello) do
      setcode do
        "Hello, world"
      end
    end
  2. Modify your site.pp file as follows:
    node 'cookbook' {
      notify { $::hello: }
    }
  3. Run Puppet:
    [root@cookbook ~]# puppet agent -t
    Notice: /File[/var/lib/puppet/lib/facter/hello.rb]/ensure: defined content as '{md5}f66d5e290459388c5ffb3694dd22388b'
    Info: Loading facts
    Info: Caching catalog for cookbook.example.com
    Info: Applying configuration version...