English 中文(简体)
Puppet - Manifest Files
  • 时间:2024-12-22

Puppet - Manifest Files


Previous Page Next Page  

In Puppet, all the programs which are written using Ruby programming language and saved with an extension of .pp are called manifests. In general terms, all Puppet programs which are built with an intension of creating or managing any target host machine is called a manifest. All the programs written in Puppet follow Puppet coding style.

The core of Puppet is the way resources are declared and how these resources are representing their state. In any manifest, the user can have a collection of different kind of resources which are grouped together using class and definition.

In some cases, Puppet manifest can even have a conditional statement in order to achieve a desired state. However, ultimately it all comes down to make sure that all the resources are defined and used in the right way and the defined manifest when appped after getting converted to a catalog is capable of performing the task for which it was designed.

Manifest File Workflow

Puppet manifest consists of the following components −

    Files (these are plain files where Puppet has nothing to do with them, just to pick them up and place them in the target location)

    Resources

    Templates (these can be used to construct configuration files on the node).

    Nodes (all the definition related to a cpent node is defined here)

    Classes

Points to Note

    In Puppet, all manifest files use Ruby as their encoding language and get saved with .pp extension.

    "Import" statement in many manifest are used for loading files when Puppet starts.

    In order to import all files contained in a directory, you can use the import statement in another way pke import cpents/* . This will import all .pp files inside that directory.

Manifest

Writing Manifests

Working with Variables

While writing a manifest, the user can define a new variable or use an existing variable at any point in a manifest. Puppet supports different kind of variables but few of them are frequently used such as strings and array of string. Apart from them, other formats are also supported.

String Variable Example

$package = "vim"  

package {  $package: 
   ensure => "installed" 
}

Using Loops

Loops are used when one wishes to go through multiple iterations on a same set of code till a defined condition is met. They are also used to do repetitive tasks with different set of values. Creating 10 tasks for 10 different things. One can create a single task and use a loop to repeat the task with different packages one wants to install.

Most commonly an array is used to repeat a test with different values.

$packages = [ vim ,  git ,  curl ]  

package { $packages: 
   ensure => "installed" 
}

Using Conditionals

Puppet supports most of the conditional structure which can be found in traditional programming languages. Condition can be used to dynamically define whether to perform a particular task or a set of code should get executed. Like if/else and case statements. Additionally, conditions pke execute will also support attributes that works pke condition, but only accepts a command output as a condition.

if $OperatingSystem !=  Linux  { 
   warning( This manifest is not supported on this other OS apart from pnux. ) 
} else { 
   notify {  the OS is Linux. We are good to go! : }
} 
Advertisements