官术网_书友最值得收藏!

  • Puppet 3 Cookbook
  • John Arundel
  • 516字
  • 2021-04-09 23:52:27

Using community Puppet style

If other people need to read or maintain your manifests, or if you want to share code with the community, it's a good idea to follow the existing style conventions as closely as possible. These govern such aspects of your code as layout, spacing, quoting, alignment, and variable references, and the official Puppet Labs recommendations on style are available at

http://docs.puppetlabs.com/guides/style_guide

How to do it…

In this section I'll show you a few of the more important examples and how to make sure that your code is style-compliant.

Indentation

Indent your manifests using two spaces (not tabs), as follows:

node 'monitoring' inherits 'server' {
  include icinga::server
  include repo::apt
}

Quoting

Always quote your resource names, for example:

package { 'exim4':

Not

package { exim4:

Use single quotes for all strings, except when:

  • The string contains variable references (for example, ${name})
  • The string contains character escape sequences (for example, \n)

In these cases you should use double quotes. Puppet doesn't process variable references or escape sequences unless they're inside double quotes.

Always quote parameter values that are not reserved words in Puppet. For example, the following values are not reserved words:

name => 'Nucky Thompson',
mode => '0700',
owner => 'deploy',

But these values are reserved words and therefore not quoted:

ensure => installed,
enable => true,
ensure => running,

Variables

Always include curly braces ({}) around variable names when referring to them in strings, for example:

source => "puppet:///modules/webserver/${brand}.conf",

Otherwise Puppet's parser has to guess which characters should be a part of the variable name and which belong to the surrounding string. Curly braces make it explicit.

Parameters

Always end lines that declare parameters with a comma, even if it is the last parameter:

service { 'memcached':
  ensure => running,
  enable => true,
}

This is allowed by Puppet, and makes it easier if you want to add parameters later, or re-order the existing parameters.

When declaring a resource with a single parameter, make the declaration all on one line and with no trailing comma:

package { 'puppet': ensure => installed }

Where there is more than one parameter, give each parameter its own line:

package { 'rake':
  ensure   => installed,
  provider => gem,
  require  => Package['rubygems'],
}

To make the code easier to read, line up the parameter arrows in line with the longest parameter, as follows:

file { "/var/www/${app}/shared/config/rvmrc":
  owner   => 'deploy',
  group   => 'deploy',
  content => template('rails/rvmrc.erb'),
  require => File["/var/www/${app}/shared/config"],
}

The arrows should be aligned per resource, but not across the whole file, otherwise it can make it difficult for you to cut and paste code from one file to another.

Symlinks

When declaring file resources which are symlinks, use ensure => link and set the target attribute, as follows:

file { '/etc/php5/cli/php.ini':
  ensure => link,
  target => '/etc/php.ini',
}

There's more…

When several people are working on a codebase, it's easy for style inconsistencies to creep in. Fortunately, there's a tool available which can automatically check your code for compliance with the style guide: puppet-lint. We'll see how to use this in the next section.

主站蜘蛛池模板: 曲阜市| 深圳市| 大姚县| 灵武市| 资中县| 将乐县| 广西| 襄城县| 浦江县| 伊春市| 临武县| 明水县| 岑溪市| 彰化市| 乌拉特前旗| 陕西省| 明光市| 天长市| 密云县| 龙井市| 长丰县| 陈巴尔虎旗| 湘乡市| 宣化县| 白河县| 宝兴县| 罗田县| 泰州市| 土默特右旗| 新沂市| 曲沃县| 横山县| 固始县| 宜兰市| 上虞市| 景德镇市| 大化| 平安县| 阿荣旗| 穆棱市| 五原县|