- 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.
- 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
- 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']], } }
- Create the file
modules/memcached/files/memcached.conf
with the following contents:-m 64 -p 11211 -u nobody -l 127.0.0.1
- Add the following to your
nodes.pp
file:node 'cookbook' { include memcached }
- 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
- 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.
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
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.
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.
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.
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
- Painter 現代服裝效果圖表現技法
- 從零開始學LATEX
- Animate 2022動畫制作:團體操隊形
- Joomla! Social Networking with JomSocial
- 中文版AutoCAD 2022基礎教程
- 中文版Photoshop CC2018從入門到精通(第4版)
- Power Query For Excel:讓工作化繁為簡
- 社會調查數據管理:基于Stata 14管理CGSS數據
- 中文版Photoshop CC基礎教程
- Professional Azure SQL Database Administration
- Science Teaching with Moodle 2.0
- Photoshop+CorelDRAW平面設計實例教程(第3版)
- 計算機圖形制作CorelDRAW X6項目教程
- Jasmine JavaScript Testing
- Troux Enterprise Architecture Solutions