Puppet 简明教程

Puppet - Manifest Files

在Puppet中,使用Ruby编程语言编写的并以 .pp 扩展名保存的所有程序都称为 Puppet代码。一般来说,所有旨在创建或管理任何目标主机机器的Puppet程序都称为清单。所有用Puppet编写的程序都遵循Puppet编码风格。

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.

Puppet的核心是声明资源的方式以及这些资源如何表示其状态。在任何清单中,用户都可以拥有一组不同类型的资源,这些资源使用类和定义分组在一起。

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.

在某些情况下,Puppet清单甚至可以包含条件语句,以便实现所需的状态。但是,最终这一切都归结为确保所有资源都得到正确的定义和使用,并且在转换为目录后应用时,定义的清单能够执行其设计任务。

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 applied after getting converted to a catalog is capable of performing the task for which it was designed.

Manifest File Workflow

Puppet清单由以下组件组成:

Puppet manifest consists of the following components −

  1. 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)

  2. Resources

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

  4. Nodes (all the definition related to a client node is defined here)

  5. Classes

Points to Note

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

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

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

manifest

Writing Manifests

Working with Variables

在编写清单时,用户可以在清单中的任何点定义新变量或使用现有变量。Puppet支持不同类型的变量,但其中几个经常使用,例如字符串和字符串数组。除了它们之外,还支持其他格式。

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

循环用于当满足已定义条件时要对同一组代码执行多次迭代。它们还用于对不同的值集执行重复性任务。为10种不同的东西创建10项任务。可以创建一个单一任务,并使用循环重复想要安装的不同包的任务。

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 支持大多数传统编程语言中的条件结构。可以使用条件来动态定义是否执行特定任务或应执行一组代码。例如 if/else 和 case 语句。此外,execute 这样的条件还将支持像条件一样工作的属性,但仅接受命令输出作为条件。

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 like execute will also support attributes that works like 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 linux.')
} else {
   notify { 'the OS is Linux. We are good to go!': }
}