Following are the steps to create an example module:
- We will use Puppet's module subcommand to create the directory structure for our new module, in our home directory (/home/vagrant):
[t@cookbook ~]$ puppet module generate thomas-memcached
- We need to create a metadata.json file for this module. Please answer the following questions; if the question is not applicable to this module, feel free to leave it blank:
Puppet uses Semantic Versioning (semver.org) to version modules.
What version is this module? [0.1.0]
-->
Who wrote this module? [thomas]
-->
What license does this module code fall under? [Apache-2.0]
-->
How would you describe this module in a single sentence?
--> A module to install memcached
Where is this module's source code repository?
--> github.com/uphillian/thomas-memcached
Where can others go to learn more about this module? [https://github.com/uphillian/thomas-memcached]
-->
Where can others go to file issues about this module? [https://github.com/uphillian/thomas-memcached/issues]
-->
{
"name": "thomas-memcached",
"version": "0.1.0",
"author": "thomas",
"summary": "A module to install memcached",
"license": "Apache-2.0",
"source": "github.com/uphillian/thomas-memcached",
"project_page": "https://github.com/uphillian/thomas
memcached",
"issues_url": "https://github.com/uphillian/thomas-memcached/issues",
"dependencies": [
{
"name": "puppetlabs-stdlib",
"version_requirement": ">= 1.0.0"
}
]
"data_provider": null
}
----------------------------------------
About to generate this metadata; continue? [n/Y]
--> y
Notice: Generating module at /home/vagrant/memcached...
Notice: Populating templates...
Finished; module generated in memcached.
memcached/spec
memcached/spec/spec_helper.rb
memcached/spec/classes
memcached/spec/classes/init_spec.rb
memcached/metadata.json
memcached/manifests
memcached/manifests/init.pp
memcached/Gemfile
memcached/examples
memcached/examples/init.pp
memcached/README.md
memcached/Rakefile
This command creates the module directory and creates some empty files as starting points.
- Now, edit memcached/manifests/init.pp and change the class definition at the end of the file to the following. Note that the puppet module created many lines of comments; in a production module, you would want to edit those default comments:
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 modules/thomas-memcached/files directory and then create a file named memcached.conf with the following contents:
[t@cookbook memcached]$ mkdir files
[t@cookbook memcached]$ echo "-m 64 -p 11211 -u nobody -l 127.0.0.1" > files/memcached.conf
- We would like this module to install memcached. We'll need to run Puppet with root privileges, and we'll use sudo for that. We'll need Puppet to be able to find the module in our home directory; we can specify this on the command line when we run Puppet, as shown in the following code snippet:
t@cookbook:memcached$ sudo /opt/puppetlabs/bin/puppet apply --modulepath=/home/vagrant -e 'include memcached'
Warning: ModuleLoader: module 'memcached' has unresolved dependencies - it will only see those that are resolved. Use 'puppet module list --tree' to see information about modules
(file & line not available)
Notice: Compiled catalog for cookbook.strangled.net in environment production in 0.46 seconds
Notice: /Stage[main]/Memcached/Package[memcached]/ensure: created
Notice: /Stage[main]/Memcached/File[/etc/memcached.conf]/ensure: defined content as '{md5}febccf4a987759cf4f1558cc625fbea9'
Notice: /Stage[main]/Memcached/Service[memcached]/ensure: ensure changed 'stopped' to 'running'
Notice: Applied catalog in 6.99 seconds
- We can verify that memcached is running using systemctl or puppet resource:
t@cookbook:memcached$ sudo /opt/puppetlabs/bin/puppet resource service memcached
service { 'memcached':
ensure => 'running',
enable => 'true',
}
t@cookbook:memcached$ sudo systemctl status memcached
memcached.service - Memcached
Loaded: loaded (/usr/lib/systemd/system/memcached.service; enabled; vendor preset: disabled)
Active: active (running) since Thu 2017-12-28 05:17:41 UTC; 3min 28s ago
Main PID: 4057 (memcached)
CGroup: /system.slice/memcached.service
└─4057 /usr/bin/memcached -u memcached -p 11211 -m 64 -c 1024
Dec 28 05:17:41 cookbook systemd[1]: Started Memcached.
Dec 28 05:17:41 cookbook systemd[1]: Starting Memcached...
Note that /opt/puppetlabs/bin/puppet may not be in root's path, use the full path or add the path to a file in /etc/profile.d.