Users and Groups
puppet
group { 'developers':
ensure => present,
gid => 3000,
}
user { 'alice':
ensure => present,
uid => 2001,
gid => 'developers', # primary group
groups => ['sudo', 'docker'], # secondary groups
shell => '/bin/bash',
home => '/home/alice',
managehome => true, # create the home directory
comment => 'Alice Smith',
password => Sensitive('$6$salt$hash...'), # a hashed password, never plain text
password_max_age => 90,
require => Group['developers'],
}
user { 'olduser':
ensure => absent, # remove (add managehome => true to delete the home)
}
Passwords: The password attribute needs a hash such as SHA-512 (openssl passwd -6), not the clear text. Keep hashes in encrypted Hiera or a secret store, and wrap them in Sensitive. For most servers, prefer SSH keys with locked passwords.
| User attribute | Meaning |
ensure | present or absent, and role for Windows/AIX |
uid, gid | Fixed IDs for consistency across servers |
groups, membership | Extra groups, and minimum (add) or inclusive (exact list) |
system | true creates a system account |
expiry | Account expiry date |
purge_ssh_keys | Remove SSH keys not managed by Puppet |
SSH Keys
puppet
ssh_authorized_key { 'alice@laptop':
ensure => present,
user => 'alice',
type => 'ssh-ed25519',
key => 'AAAAC3NzaC1lZDI1NTE5AAAAIExampleKeyDataHere',
options => ['no-port-forwarding'],
}
user { 'deploy':
ensure => present,
managehome => true,
purge_ssh_keys => true, # only keys managed by Puppet stay
}
Put the public key only, never the private key. The key is just the middle part of the line from id_ed25519.pub.
A Reusable Defined Type for Accounts
puppet
define profile::account (
Integer $uid,
Array[String] $groups = [],
Array[Hash] $ssh_keys = [],
String $shell = '/bin/bash',
) {
user { $title:
ensure => present,
uid => $uid,
groups => $groups,
shell => $shell,
managehome => true,
purge_ssh_keys => true,
}
$ssh_keys.each |$k| {
ssh_authorized_key { "${title}-${k['name']}":
user => $title,
type => $k['type'],
key => $k['key'],
}
}
}
# Data-driven from Hiera
lookup('profile::accounts', Hash, 'deep', {}).each |$name, $cfg| {
profile::account { $name: * => $cfg }
}
Cron Jobs
puppet
cron { 'nightly-backup':
ensure => present,
command => '/usr/local/bin/backup.sh >> /var/log/backup.log 2>&1',
user => 'root',
hour => 2,
minute => 30,
weekday => '*',
}
cron { 'every-5-min-check':
command => '/usr/local/bin/check.sh',
minute => '*/5',
}
cron { 'monthly-report':
command => '/usr/local/bin/report.sh',
monthday => 1,
hour => 6,
minute => 0,
}
cron { 'old-job':
ensure => absent,
}
For modern Linux systems, many teams create systemd timers by managing unit files with the file resource (plus the systemd module), but cron is perfectly fine for simple jobs.
exec: Run a Command (Use Carefully)
exec runs a command. It is powerful but not idempotent by itself. You must tell Puppet when the command is needed, or it will run on every run.
| Attribute | Meaning |
command | The command. Use a full path or set path |
path | Directories to search, such as ['/bin','/usr/bin','/usr/local/bin'] |
creates | Run only if this file does not exist |
onlyif | Run only if this command returns 0 |
unless | Run only if this command returns non-zero |
refreshonly | Run only when notified by another resource |
cwd, user, environment | Working directory, user, and env variables |
timeout | Seconds before giving up (default 300) |
returns | Accepted exit codes (default 0) |
logoutput | on_failure, true: show command output |
puppet
# 1. creates: run once, until the file exists
exec { 'extract-app':
command => 'tar -xzf /tmp/app.tar.gz -C /opt',
path => ['/bin', '/usr/bin'],
creates => '/opt/app/bin/app',
}
# 2. unless: run only if the check fails
exec { 'add-swap':
command => '/sbin/mkswap /swapfile && /sbin/swapon /swapfile',
unless => '/sbin/swapon --show | /bin/grep -q swapfile',
}
# 3. onlyif: run only if something is true
exec { 'reload-firewall':
command => 'firewall-cmd --reload',
path => ['/usr/bin'],
onlyif => 'systemctl is-active firewalld',
}
# 4. refreshonly: run when a file changes
exec { 'rebuild-index':
command => '/usr/local/bin/reindex.sh',
refreshonly => true,
subscribe => File['/etc/app/index.conf'],
}
Last resort: Before you use exec, search for a resource type or Forge module that already does the job. If you must use exec, add creates, unless, onlyif or refreshonly, otherwise the command runs every 30 minutes.
Mounts and Filesystems
puppet
file { '/data':
ensure => directory,
}
mount { '/data':
ensure => mounted, # in fstab and mounted now
device => 'UUID=1234-ABCD',
fstype => 'ext4',
options => 'defaults,noatime',
dump => 0,
pass => 2,
require => File['/data'],
}
mount { '/mnt/share':
ensure => mounted,
device => 'nfs.example.com:/exports/share',
fstype => 'nfs',
options => 'rw,hard,intr',
}
| Mount ensure | Effect |
defined | Only in fstab |
mounted | In fstab and mounted |
unmounted | In fstab, not mounted |
absent | Removed from fstab and unmounted |
Hosts, Kernel Settings and Others
puppet
host { 'db.internal':
ensure => present,
ip => '10.0.2.15',
host_aliases => ['db', 'database'],
}
# Kernel parameters: use the Forge module herculesteam/augeasproviders_sysctl (or write /etc/sysctl.d/)
file { '/etc/sysctl.d/90-puppet.conf':
ensure => file,
content => "net.ipv4.ip_forward = 1\nvm.swappiness = 10\n",
notify => Exec['sysctl-reload'],
}
exec { 'sysctl-reload':
command => '/sbin/sysctl --system',
refreshonly => true,
}
Other Built-in Types Worth Knowing
| Type | Purpose |
notify | Print a message in the run log (a resource, different from the notify metaparameter) |
schedule | Limit resources to time windows or a number of runs |
filebucket | Backup storage for changed files |
tidy | Remove old files by age or size |
yumrepo | Define a YUM/DNF repository |
selboolean, selmodule | SELinux settings |
zfs, zpool | ZFS management |
k5login, sshkey | Kerberos and SSH host keys |
augeas | Edit structured config files using Augeas lenses |
puppet
tidy { '/var/log/myapp':
age => '14d',
recurse => true,
matches => '*.log',
}
yumrepo { 'internal':
ensure => present,
descr => 'Internal packages',
baseurl => 'https://repo.example.com/el9/',
gpgcheck => 1,
gpgkey => 'https://repo.example.com/RPM-GPG-KEY',
enabled => 1,
}
notify { "Running on ${facts['networking']['fqdn']}": }
See All Types
bash
puppet describe --list # all resource types
puppet describe user # attributes and help for one type
puppet describe user --meta # include metaparameters
Practice Tasks
- Create the group
developers and three users in it, with home directories
- Add an SSH public key for one user and check
~/.ssh/authorized_keys
- Make a cron job that appends a timestamp to a file every five minutes and check it works
- Write an
exec that creates a file only once using creates, then run Puppet twice to see it skipped
- Mount a tmpfs at
/mnt/ramdisk with Puppet and check with df -h
Conclusion
Core types manage accounts (user, group, ssh_authorized_key), schedules (cron), commands (exec) and storage (mount). Make every exec idempotent with creates, unless, onlyif or refreshonly.
Quick Summary: user+group+ssh key; cron; exec needs creates/unless/onlyif/refreshonly; mount ensure mounted; puppet describe.