Classes
Getting Started
Puppet class is just a block of puppet code, which use to declare resources on you system. Class has parameters, which allow you to adapt a class to suit your needs. We will cover variable and parameters in the next section, for now, let's focus on how the abstraction provided by classes allows you to manage complex sets of resources in terms of a single function they serve.
How to use classes?
Definition
First we need to define a class and saving it to a manifest, and Puppet will parse the manifest and remember your class definition.
Invocation
Once you class defined, and we can invoke your class definitions with or with not parameters.
There are two ways to invoke your Puppet class, if you're using Puppet Enterprise Console, and have all your class invocation placed in you module-path, then it will be trigger to invoke every 30 minutes(base on you configuration, which actually executed by puppet agent -t
), another way is manually execute puppet apply command like:
puppet apply file-of-puppet-code.pp
For this tutorial, we will just use puppet apply
statement as we use demo on puppet node, no Puppet Enterprise Console.
Validation
For any Puppet manifest, we can use Puppet tool parser
to validate your code:
puppet parser validate file-of-puppet-code.pp
Singleton
In Puppet, classes are singleton, which means that a class can only be declared once on a given node. In this sense, Puppet's classes are different than the kind of classes you may have encountered in object-oriented programming, which are often instantiated multiple times. Declaring a class multiple times could give Puppet conflicting instructions for how to manage resources on a system.
Learning by Doing
User Creation
Below tutorial will guide you to define a puppet class to create user
Find you module path (For modules, we will discuss in the next section)
Execute below code to print out you module-path
puppet master --configprint modulepath
Which would result:
/etc/puppetlabs/code/environments/production/modules:/etc/puppetlabs/code/modules:/opt/puppetlabs/puppet/modules
/etc/puppetlabs/code/environments/production/modules
are used to save user defined modules, or keep modules for your prod environment.
/etc/puppetlabs/code/modules
is for site specific modules.
/opt/puppetlabs/puppet/modules
is for modules required by puppet itself.
Go to production module-path:
cd /etc/puppetlabs/code/environments/production/modules
Create a folder
mkdir user_creation/{manifests,examples,files} -p
Class Definition
Create you first class definition by vim user_creation/manifests/init.pp
with below content:
class user_creation {
user {'Erdan':
ensure => present ,
home => '/home/erdan',
managehome => true,
}
}
Compose a class invocation, vim user_creation/examples/user.pp
with content:
include user_creation
Validation
Validate you class and you class invocation
puppet parser validate user_creation/manifests/init.pp
puppet parser validate user_creation/examples/user.pp
If no error, then no output.
Dry Run
Before applying any changes to your system, it's always a good idea to use the --noop
flag to do a 'dry run' of the Puppet agent. This will compile the catalog and notify you of the changes that Puppet would have made without actually applying any of those changes to your system.
puppet apply --noop user_creation/examples/user.pp
If your dry run looks good, go ahead and run puppet apply again without the --noop
flag.
Verify
If everything goes on well, then you can see the user created by puppet
cat /etc/passwd|grep erdan
User home also created.
ls /home/erdan