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

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

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
主站蜘蛛池模板: 和硕县| 鸡东县| 桓仁| 武鸣县| 沙雅县| 扎赉特旗| 临海市| 自治县| 金溪县| 威宁| 安乡县| 桂东县| 张家界市| 新巴尔虎右旗| 五常市| 白沙| 六枝特区| 个旧市| 定远县| 天峻县| 海盐县| 砚山县| 深州市| 枣庄市| 仁怀市| 香河县| 察隅县| 新余市| 顺平县| 浦城县| 兖州市| 白水县| 博野县| 乌海市| 富阳市| 静安区| 抚顺市| 翁牛特旗| 滨州市| 灵璧县| 天长市|