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

Using modules

One of the most important things you can do to make your Puppet manifests clearer and more maintainable is to organize them into modules.

A module is simply a way of grouping related things: for example, a webserver module might include everything necessary for a machine to be a web server: Apache configuration files, virtual host templates, and the Puppet code necessary to deploy these.

Separating things into modules makes it easier to re-use and share code; it's also the most logical way to organize your manifests. In this example we'll create a module to manage memcached, a memory caching system commonly used with web applications.

How to do it…

Here are the steps to create an example module.

  1. Create the following new directories in your Puppet repo:
    ubuntu@cookbook:~/puppet$ mkdir modules/memcached
    ubuntu@cookbook:~/puppet$ mkdir modules/memcached/manifests
    ubuntu@cookbook:~/puppet$ mkdir modules/memcached/files
    
  2. Create the file modules/memcached/manifests/init.pp with the following contents:
    # Manage memcached
    class memcached {
      package { 'memcached':
        ensure => installed,
      }
    
      file { '/etc/memcached.conf':
        source  => 'puppet:///modules/memcached/memcached.conf',
        owner   => 'root',
        group   => 'root',
        mode    => '0644',
        require => Package['memcached'],
      }
    
      service { 'memcached':
        ensure  => running,
        enable  => true,
        require => [Package['memcached'],
                    File['/etc/memcached.conf']],
      }
    }
  3. Create the file modules/memcached/files/memcached.conf with the following contents:
    -m 64
    -p 11211
    -u nobody
    -l 127.0.0.1
  4. Add the following to your nodes.pp file:
    node 'cookbook' {
      include memcached
    }
  5. Run Puppet to test the new configuration:
    ubuntu@cookbook:~/puppet$ papply
    Notice: /Stage[main]/Memcached/Package[memcached]/ensure:
     created
    Notice: /Stage[main]/Memcached/File[/etc/memcached.conf]/content:
     content changed '{md5}58c9e04b29e08c2e9b3094794d3ebd0e'
     to '{md5}9429eff3e3354c0be232a020bcf78f75'
    Notice: Finished catalog run in 4.54 seconds
    
  6. Check whether the new service is running:
    ubuntu@cookbook:~/puppet$ service memcached status
    * memcached is running
    

How it works…

Modules have a specific directory structure. Not all of these directories need to be present, but if they are, this is how they should be organized:

modules/
    MODULE_NAME/
        files/
        templates/
        manifests/

All manifest files (those containing Puppet code) live in the manifests directory. In our example, the memcached class is defined in the file manifests/init.pp, which will be imported automatically.

Inside the memcached class, we refer to the memcached.conf file:

file { '/etc/memcached.conf':
  source => 'puppet:///modules/memcached/memcached.conf',
}

As we saw in the section on Puppet's file server and custom mount points, the preceding source parameter tells Puppet to look for the file in

MODULEPATH/
    memcached/
        files/
            memcached.conf

There's more…

Learn to love modules, because they'll make your Puppet life a lot easier. They're not complicated. However, practice and experience will help you judge when things should be grouped into modules, and how best to arrange your module structure. Here are a few tips which may help you on the way.

Templates

If you need to use a template as a part of the module, place it in the module's templates directory and refer to it as follows:

file { '/etc/memcached.conf':
  content => template('memcached/memcached.conf.erb'),
}

Puppet will look for the file in:

MODULEPATH/
    memcached/
        templates/
          memcached.conf.erb

Facts, functions, types, and providers

Modules can also contain custom facts, custom functions, custom types, and providers. For more information about these, refer to Chapter 8, External Tools and the Puppet Ecosystem.

Autogenerating module layout

You can also use the puppet module generate command to generate the directory layout for new modules, rather than doing it by hand. See the Using public modules section in Chapter 8, External Tools and the Puppet Ecosystem, for more details.

Third party modules

You can download modules provided by other people and use them in your own manifests just like the modules you create. For more on this, see the section on using public modules.

Module organization

For more details on how to organize your modules, see the Puppet Labs website:

http://docs.puppetlabs.com/puppet/3/reference/modules_fundamentals.html

See also

  • The Creating custom facts recipe in Chapter 8, External Tools and the Puppet Ecosystem
  • The Using public modules recipe in Chapter 8, External Tools and the Puppet Ecosystem
  • The Creating your own resource types recipe in Chapter 8, External Tools and the Puppet Ecosystem
  • The Creating your own providers recipe in Chapter 8, External Tools and the Puppet Ecosystem
主站蜘蛛池模板: 舒城县| 玉田县| 米脂县| 原平市| 容城县| 登封市| 沽源县| 天柱县| 景谷| 吉林省| 合山市| 板桥市| 阿城市| 潮州市| 三台县| 夹江县| 呼伦贝尔市| 甘谷县| 涞源县| 武强县| 克山县| 建湖县| 凉城县| 石台县| 黑龙江省| 盘山县| 九寨沟县| 峨边| 额敏县| 甘肃省| 云龙县| 全州县| 上饶县| 鸡泽县| 丹凤县| 四会市| 海阳市| 武宁县| 阳原县| 郁南县| 新平|