Tags Archives: facter

An Overview of Puppet

Puppet is based on a client/server architecture where one machine in a Puppet network acts as the server machine with Puppet server software running on it, while the remaining machines act as clients with Puppet agent software running on them.

 

All Puppet manifests are written in the Ruby programming language and saved with an extension of .pp.

 

Puppet works by using manifests to define resources and the state of these resources. Resources can be grouped together using class and definition.

 

A Puppet manifest consists of the following:

 

Files

 

Resources

 

Templates (used to create configuration files on puppet nodes).

 

Nodes (definitions relating to client nodes are located here)

 

Classes

 

 

 

The import statement

 

The “Import” statement found in many manifests is used for loading files when Puppet starts. For example, to import all files contained in a directory, you can use the import statement:

 

import ‘clients/*’.

 

This will import all .pp files stored in that directory.

 

 

Variables

 

A manifest can define a new variable or use an existing variable. Puppet supports many kinds of variables. Example:

 

String Variable Example

 

$package = “ssh”

 

package { $package:
ensure => “installed”
}

 

 

This ensures the package ssh is available and installed.

 

 

Loops

 

Loops are used in order to apply multiple iterations on a section of code until a specific defined condition is met.

They can also be delpoyed to execute repetitive tasks using different set of values. For example, you may wish to create a single task process and then deploy a loop to repeat the task for the different software packages you wish to install.

 

An array is especially useful for repeating a test with different values. For example:

 

$packages = [‘ssh’, ‘vim’, ‘git’]

 

package { $packages:
ensure => “installed”
}

 

This ensures the packages ssh, vim and git are all installed.

 

Conditionals

 

Conditions can be created and applied to dynamically define whether a task or section of code should be executed and a specific action taken.

 

An execute condition supports attributes that work similarly to a simple condition, but only accepting a command output as a condition.

 

Example:

 

if $OperatingSystem != ‘Linux’ {
warning(‘Only Linux OS distributions are supported’)
} else {
notify { ‘The installed OS found is Linux’: }
}

 

 

Modules

 

A Puppet module is a collection of resources, classes, files, definition, and templates.

 

This enables generic modules to be written and used multiple times with very few code alterations.

 

 

Module Configuration

 

The search path for modules is defined using a colon-separated list of directories in the puppetmasterd or masterd using the modulepath parameter:

 

[puppetmasterd]
modulepath = /var/lib/puppet/modules:/data/puppet/modules

 

The search path can also be defined at runtime by setting the PUPPETLAB environment variable (as a colon-separated list of variables).

 

 

Module Internal Organization

 

When you create a new Puppet module, it uses the following structure and contains manifest, distributed file, plugins, and templates arranged in a specific directory structure:

 

MODULE_PATH/
downcased_module_name/
files/
manifests/
init.pp
lib/
puppet/
parser/
functions
provider/
type/
facter/
templates/
README

 

Whenever a module is created, it contains an init.pp manifest file at the specified location in the manifests directory.

 

This manifest file always executes first and contains all the classes associated with that particular module.

 

Additional .pp files can be added directly under the manifests folder. These should be named after the class.

 

Puppet deploys at standalone tool called facter which holds environment level variables, similar to the env variables of a Bash shell.

 

In Puppet, the key-value pair is known as a “fact”. Each resource has its own facts and Puppet users can also create their own custom facts.

 

facter

 

The facter command can be used to list all environment variables and their values. These are built-in with facter out-of-the-box and are referred to as core facts.

 

Users can then add custom facts to the facter collection.

 

To view a variable using facter:

 

facter {Variable Name}

 

Example
[root@puppetmaster ~]# facter virtual
virtualbox

 

Puppet has three types of fact that can be used and defined:

 

Core Facts
Custom Facts
External Facts

 

Core facts are defined at the top level and are accessible to all at any point in the Puppet code system.

 

Before a Puppet agent requests for a catalog from the puppetmaster, the agent first deploys facter to compile a list of information available in itself in the form of a key value pair. Each key-value pair is referred as a fact.

 

Facts can be referenced in manifests as normal variables by using the $ prefix.

 

Example:

 

if ($OperatingSystem == “Linux”) {
$message = “This OS is $OperatingSystem \n”
} else {
$message = “This is an unknown machine\n”
}

 

file { “/tmp/machineOperatingSystem.txt”:
ensure => file,
content => “$message”
}

 

You can also write custom facts in Puppet. For information on how to do this, see the following link on the Puppet site:

 

https://docs.puppet.com/facter/latest/fact_overview.html#writing-structured-facts

 

Continue Reading