Simplest Puppet Install Pattern

Version 12 (Nan Liu, 10/31/2011 09:52 am)

1 4 James Turnbull
# Getting Started with a Simple Puppet Pattern
2 1
3 11 Nan Liu
Using Puppet is largely about developing the Puppet manifests, which describe the desired configuration of your system.
4 1
5 11 Nan Liu
Once you have Puppet installed (see [Installation Guide](http://docs.puppetlabs.com/guides/installation.html)) on at least one server, you're ready to set up a minimal configuration and get started using Puppet. Using puppet in agent master mode is also supported for more than one system.
6 1
7 6 Matt Griffith
If you want to test your Puppet manifests without installing a client and server you can run:
8 6 Matt Griffith
9 11 Nan Liu
    puppet apply /path/to/puppet/test/file.pp
10 6 Matt Griffith
11 1
# A Simple Manifest: Managing Ownership of a File
12 1
13 1
For our first manifest, we'll manage a single resource (in this
14 1
case, a file) on all of our hosts.
15 1
16 12 Nan Liu
## Step one: Create sudo manifests
17 1
18 12 Nan Liu
First, we'll create a class in a puppet manifest under a sudo module. The puppet autoloader convention require all modules contain an init.pp file that contains a class or define that matches the module name. We'll use this appropriately-named manifest for all configuration information related to sudo, so next time we'll be able to find the sudo stuff quickly. We'll start simple, and just manage the sudoers file for now:
19 1
20 11 Nan Liu
    # /etc/puppet/modules/sudo/manifests/init.pp
21 1
    
22 1
    class sudo {
23 1
        file { "/etc/sudoers":
24 11 Nan Liu
            owner => 'root',
25 11 Nan Liu
            group => 'root',
26 11 Nan Liu
            mode  => '0440',
27 1
        }
28 1
    }
29 1
30 11 Nan Liu
So now we have a class which contains a single file resource that will ensure that the owner, group, and mode of the /etc/sudoers file will be set consistently across all systems that includes that class (but we haven't chosen which computers belong in that class, yet).
31 1
32 11 Nan Liu
## Step two: Create a test manifests.
33 1
34 11 Nan Liu
In order to test the class we just created, add a test manifest to the sudo module:
35 1
36 11 Nan Liu
    # /etc/puppet/modules/sudo/tests/init.pp
37 11 Nan Liu
    
38 11 Nan Liu
    # This is the same as 0.25.x
39 11 Nan Liu
    # include sudo
40 11 Nan Liu
    class { 'sudo': }
41 11 Nan Liu
42 11 Nan Liu
Next, we can simulate the sudo class by applying the test manifests with the noop flag:
43 11 Nan Liu
44 11 Nan Liu
    puppet apply --noop /etc/puppet/modules/sudo/tests/init.pp
45 11 Nan Liu
46 11 Nan Liu
If the system configuration is different than the specification in the sudo class, you should see what puppet would have changed during the --noop run, and you can apply those changes by removing noop, so puppet is no longer simulating the changes, but enforcing the configuration against the system:
47 11 Nan Liu
48 11 Nan Liu
    puppet apply /etc/puppet/modules/sudo/tests/init.pp
49 11 Nan Liu
50 11 Nan Liu
## Step three: Create /etc/puppet/manifests/site.pp
51 11 Nan Liu
52 11 Nan Liu
Now we'll create the site.pp manifest which is the master manifest. Puppet will search for a manifest by this name by default. Here's our file:
53 11 Nan Liu
54 1
    # /etc/puppet/manifests/site.pp
55 1
    
56 1
    node default {
57 1
        include sudo
58 1
    }
59 10 Nan Liu
60 11 Nan Liu
After we create a default node definition, the default node definition will be applied to any node that doesn't fall into any other node definition's scope. In this case, since we have no other node definitions, all
61 11 Nan Liu
puppet agents will follow this node definition and so any node will include our sudo class.
62 8 James Turnbull
63 11 Nan Liu
## Step four: Start the Puppetmaster
64 1
65 11 Nan Liu
Now that we have a basic manifest configuration laid out, we can start the Puppet master daemon. The --mkusers option is only necessary the method used to install puppet didn't create the puppet user and group:
66 1
67 1
    master % sudo puppet master --mkusers
68 1
69 11 Nan Liu
This will background the daemon and send all of its logs to the syslog facility. If you would prefer, you can add --verbose --no-daemonize and the daemon will stay in the foreground and its messages will go to the terminal.
70 1
71 11 Nan Liu
On Ubuntu and other Debian based distributions, starting the Puppetmaster daemon works like this:
72 1
73 1
    master % sudo /etc/init.d/puppetmaster restart
74 1
75 11 Nan Liu
## Step five: Run a client
76 1
77 11 Nan Liu
It's usually best to start with your first client being Puppetmaster server itself. However, since the Puppetmaster will be
78 1
talking to itself, that client will already have a certificate, so
79 1
no signing will be necessary to establish trust between the
80 1
Puppetmaster server and itself. In this example, we'll configure a
81 1
client that isn't the Puppetmaster server so we can demonstrate how
82 1
to establish cryptographic trust between the Puppetmaster server
83 1
and its new clients:
84 1
85 11 Nan Liu
First, start puppet agent on the client in verbose mode:
86 1
87 8 James Turnbull
    client% sudo puppet agent --verbose
88 1
89 1
You should see a message about not receiving a certificate, and on
90 1
the server you should get a message about a request waiting for
91 1
you. On the server, we'll list the certificates waiting for
92 1
signatures:
93 1
94 8 James Turnbull
    master% sudo puppet cert --list
95 1
96 1
You should see our client's name listed, so we can give the
97 1
Puppetmaster the command to sign its certificate (thus creating a
98 1
trust relationship that client):
99 1
100 8 James Turnbull
    master% sudo puppet cert --sign <client>
101 1
102 1
Within two minutes (the default value for --waitforcert), the
103 1
client should connect again and receive its signed certificate.
104 1
Once the signed cert is in place, the client should ask for its
105 1
configuration; the server will compile it (and log that it has done
106 1
so) and pass the compiled configuration to the client.
107 1
108 1
Then, if the sudoers file had incorrect permissions, we should see
109 1
one or more messages indicating the corrections; but if everything
110 1
about the sudoers file is already correct, you'll just see messages
111 1
about starting and ending the configuration run.
112 1
113 1
If you restart puppetd without the --verbose option, it will
114 1
background itself and run periodically thereafter, checking with
115 1
the Puppetmaster server to see if there the manifests have
116 1
changed.
117 1
118 1
## Caveat
119 1
120 1
This is half of the infrastructure for a working Puppet
121 1
installation. The other half of a hygienic and efficient
122 1
installation revolves around version-controlling the Puppet
123 1
manifests and the files Puppet will be managing. It also provides a
124 1
minimalist structure around which to build other Puppet classes.
125 1
126 1
## Where to next
127 1
128 5 James Turnbull
Have a look at the [[Advanced Puppet Pattern]] to see how we can
129 1
build on this basic recipe.