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

  • DevOps:Puppet,Docker,and Kubernetes
  • Thomas Uphill John Arundel Neependra Khare Hideto Saito Hui Chuan Chloe Lee Ke Jou Carol Hsu
  • 520字
  • 2021-07-09 18:32:48

Checking your manifests with Puppet-lint

The puppetlabs official style guide outlines a number of style conventions for Puppet code, some of which we've touched on in the preceding section. For example, according to the style guide, manifests:

  • Must use two-space soft tabs
  • Must not use literal tab characters
  • Must not contain trailing white space
  • Should not exceed an 80 character line width
  • Should align parameter arrows (=>) within blocks

Following the style guide will make sure that your Puppet code is easy to read and maintain, and if you're planning to release your code to the public, style compliance is essential.

The puppet-lint tool will automatically check your code against the style guide. The next section explains how to use it.

Getting ready

Here's what you need to do to install Puppet-lint:

  1. We'll install Puppet-lint using the gem provider because the gem version is much more up to date than the APT or RPM packages available. Create a puppet-lint.pp manifest as shown in the following code snippet:
      package {'puppet-lint':
        ensure => 'installed',
        provider => 'gem',
      }
  2. Run puppet apply on the puppet-lint.pp manifest, as shown in the following command:
    t@cookbook ~$ puppet apply puppet-lint.pp Notice: Compiled catalog for node1.example.com in environment production in 0.42 seconds
    Notice: /Stage[main]/Main/Package[puppet-lint]/ensure: created
    Notice: Finished catalog run in 2.96 seconds
    t@cookbook ~$ gem list puppet-lint *** LOCAL GEMS *** puppet-lint (1.0.1)
    

How to do it...

Follow these steps to use Puppet-lint:

  1. Choose a Puppet manifest file that you want to check with Puppet-lint, and run the following command:
    t@cookbook ~$ puppet-lint puppet-lint.pp 
    WARNING: indentation of => is not properly aligned on line 2
    ERROR: trailing whitespace found on line 4
    
  2. As you can see, Puppet-lint found a number of problems with the manifest file. Correct the errors, save the file, and rerun Puppet-lint to check that all is well. If successful, you'll see no output:
    t@cookbook ~$ puppet-lint puppet-lint.pp 
    t@cookbook ~$
    

There's more...

You can find out more about Puppet-lint at https://github.com/rodjek/puppet-lint.

Should you follow Puppet style guide and, by extension, keep your code lint-clean? It's up to you, but here are a couple of things to think about:

  • It makes sense to use some style conventions, especially when you're working collaboratively on code. Unless you and your colleagues can agree on standards for whitespace, tabs, quoting, alignment, and so on, your code will be messy and difficult to read or maintain.
  • If you're choosing a set of style conventions to follow, the logical choice would be that issued by puppetlabs and adopted by the community for use in public modules.

Having said that, it's possible to tell Puppet-lint to ignore certain checks if you've chosen not to adopt them in your codebase. For example, if you don't want Puppet-lint to warn you about code lines exceeding 80 characters, you can run Puppet-lint with the following option:

t@cookbook ~$ puppet-lint --no-80chars-check

Run puppet-lint --help to see the complete list of check configuration commands.

See also

  • The Automatic syntax checking with Git hooks recipe in Chapter 2, Puppet Infrastructure
  • The Testing your Puppet manifests with rspec-puppet recipe in Chapter 9, External Tools and the Puppet Ecosystem
主站蜘蛛池模板: 宝坻区| 普安县| 内乡县| 平南县| 南昌市| 玛多县| 恩平市| 清丰县| 阿拉善左旗| 新平| 荥阳市| 巴里| 临洮县| 济宁市| 文昌市| 大埔区| 鹿泉市| 镇坪县| 邯郸市| 科技| 鄂托克旗| 松溪县| 海盐县| 长乐市| 如皋市| 肥西县| 绥滨县| 卢龙县| 宁南县| 团风县| 封丘县| 涟水县| 那坡县| 阜城县| 南澳县| 淮安市| 青神县| 汉沽区| 宁乡县| 措勤县| 井冈山市|