Puppet Module Structure: Layout, Autoloading, Files, Templates and metadata.json

Puppet Training Course 20 min min read Updated: Nov 17, 2026 Beginner
Puppet Module Structure: Layout, Autoloading, Files, Templates and metadata.json
Beginner Topic 1 of 4

What Is a Module?

A module is a directory with a standard structure that holds classes, defined types, files, templates, facts and more for one purpose, for example ntp, nginx or profile. Modules are the unit of sharing and reuse in Puppet.

Standard Layout

text mymodule/ metadata.json module name, version, dependencies, OS support README.md REFERENCE.md generated docs (puppet strings) manifests/ init.pp class mymodule install.pp class mymodule::install config.pp class mymodule::config service.pp class mymodule::service vhost.pp define mymodule::vhost params/ (nested names: mymodule::params::x) files/ app.conf puppet:///modules/mymodule/app.conf templates/ app.conf.epp epp('mymodule/app.conf.epp') data/ common.yaml module-level Hiera defaults os/RedHat.yaml hiera.yaml module data configuration lib/ facter/my_fact.rb custom facts puppet/functions/ custom Ruby functions puppet/type/ custom resource types puppet/provider/ providers functions/ functions written in Puppet language types/ type aliases tasks/ Bolt tasks plans/ Bolt plans examples/ sample manifests spec/ unit tests

Autoloading: Names Map to Files

Puppet finds code by name, so the file path must match. If it does not, you get Could not find class.

Name in codeFile that must contain it
class mymodulemanifests/init.pp
class mymodule::installmanifests/install.pp
class mymodule::config::tlsmanifests/config/tls.pp
define mymodule::vhostmanifests/vhost.pp
mymodule::my_function()functions/my_function.pp or lib/puppet/functions/mymodule/my_function.rb
Type alias Mymodule::Porttypes/port.pp
puppet:///modules/mymodule/a.conffiles/a.conf
epp('mymodule/a.epp')templates/a.epp
Naming rules: Module names use lowercase letters, digits and underscores, start with a letter and must not contain dashes. The directory name must be the module name only (mymodule), not author-mymodule.

A Small Complete Module

puppet # manifests/init.pp class ntp ( Array[String] $servers = ['pool.ntp.org'], String $package = 'chrony', String $service = 'chronyd', ) { contain ntp::install contain ntp::config contain ntp::service Class['ntp::install'] -> Class['ntp::config'] ~> Class['ntp::service'] } # manifests/install.pp class ntp::install { package { $ntp::package: ensure => installed, } } # manifests/config.pp class ntp::config { file { '/etc/chrony.conf': ensure => file, mode => '0644', content => epp('ntp/chrony.conf.epp', { 'servers' => $ntp::servers }), } } # manifests/service.pp class ntp::service { service { $ntp::service: ensure => running, enable => true, } }

The classic install, config, service split makes ordering clear, and each class stays small. Sub-classes read the main class parameters as $ntp::servers. Mark helper classes as private in the documentation, because users should only declare ntp.

metadata.json

json { "name": "myorg-ntp", "version": "1.2.0", "author": "My Org", "summary": "Manage time synchronization", "license": "Apache-2.0", "source": "https://github.com/myorg/puppet-ntp", "dependencies": [ { "name": "puppetlabs/stdlib", "version_requirement": ">= 9.0.0 < 10.0.0" } ], "operatingsystem_support": [ { "operatingsystem": "Ubuntu", "operatingsystemrelease": ["22.04", "24.04"] }, { "operatingsystem": "Rocky", "operatingsystemrelease": ["8", "9"] } ], "requirements": [ { "name": "puppet", "version_requirement": ">= 7.0.0 < 9.0.0" } ] }

metadata.json lists dependencies and the OS and Puppet versions you support. Tools such as the PDK, rspec-puppet and the Forge read it. Use the Forge style name author-module inside metadata, and the plain module name as the directory.

Files and Templates Inside a Module

puppet file { '/etc/ntp.motd': source => 'puppet:///modules/ntp/motd.txt', # modules/ntp/files/motd.txt } file { '/etc/chrony.conf': content => epp('ntp/chrony.conf.epp', { 'servers' => $servers }), # modules/ntp/templates/chrony.conf.epp } file { '/etc/ntp/extra.conf': content => file('ntp/extra.conf'), # read text of a file from files/ }

Module Data (Hiera in a Module)

yaml # ntp/hiera.yaml --- version: 5 defaults: datadir: data data_hash: yaml_data hierarchy: - name: 'OS family' path: 'os/%{facts.os.family}.yaml' - name: 'common' path: 'common.yaml' # ntp/data/os/RedHat.yaml --- ntp::package: chrony ntp::service: chronyd # ntp/data/os/Debian.yaml --- ntp::package: chrony ntp::service: chrony

This is the module layer of Hiera: sensible defaults per OS that ship with the module. Your control repository can override them in the environment or global layer. This replaces the old params.pp pattern.

Where Puppet Looks for Modules

bash puppet config print modulepath # On a server: /etc/puppetlabs/code/environments/production/modules:/etc/puppetlabs/code/environments/production/site-modules:/etc/puppetlabs/code/modules:/opt/puppetlabs/puppet/modules puppet module list --tree

In a typical control repository, site-modules/ holds your own modules (profiles, roles), and modules/ holds Forge modules installed by r10k from the Puppetfile.

Quick Local Development

bash mkdir -p ~/work/modules && cd ~/work/modules mkdir -p ntp/manifests ntp/templates # write ntp/manifests/init.pp ... puppet parser validate ntp/manifests/*.pp puppet apply --modulepath ~/work/modules -e 'include ntp' --noop puppet apply --modulepath ~/work/modules -e 'class { "ntp": servers => ["time.example.com"] }' puppet strings generate --format markdown # create REFERENCE.md (needs puppet-strings gem)

Documenting With Puppet Strings

puppet # @summary Manage time synchronization. # # @example Default usage # include ntp # # @param servers # List of time servers to use. # @param package # Name of the package. class ntp ( Array[String] $servers = ['pool.ntp.org'], String $package = 'chrony', ) { }

Best Practices

  • One purpose per module. A module that manages everything is hard to reuse
  • Keep the public interface small: a main class with typed parameters
  • Use contain for sub-classes and -> or ~> between them
  • Prefer data in data/ over params.pp
  • Always set owner, group and mode for files
  • Add tests (rspec-puppet) and run puppet-lint
  • Version modules with semantic versioning

Practice Tasks

  1. Create the ntp module above by hand and run it with puppet apply --modulepath
  2. Break the layout on purpose (put a class in the wrong file) and read the error
  3. Add hiera.yaml and OS data files so package and service names come from data
  4. Write a README.md and a @param comment block for every parameter
  5. Create a files/ item and a templates/ item and use both

Conclusion

A module is a standard directory layout where class names map to file paths. Use init.pp, install-config-service classes, metadata.json, and module data in data/ to keep modules small and reusable.

Quick Summary: class name = file path; init.pp; files/ and templates/; metadata.json; module Hiera data; site-modules vs modules.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators