Variables
Variables start with $. They can be assigned only once in a scope, so you cannot change them later. This keeps code predictable.
| Rule | Detail |
|---|---|
| Names | Lowercase letters, digits and underscores, starting with a letter or underscore: $web_port |
| Interpolation | Only in double quotes: "Port is ${port}" |
| Hash and array access | $app['name'], $packages[0], and for nested data $facts['os']['family'] |
| Qualified names | $apache::port reads a variable from another class |
| Constants | No special type. By convention just use lower case and do not change |
Data Types
| Type | Examples | Notes |
|---|---|---|
String | 'text', "hi ${x}" | Single quotes are literal, double quotes interpolate |
Integer, Float, Numeric | 42, 3.14, 0x1F | Arithmetic works |
Boolean | true, false | No quotes |
Undef | undef | Absence of a value |
Array | [1, 2, 3] | Zero-based index |
Hash | { 'a' => 1 } | Key-value, ordered by insertion |
Regexp | /^web\d+$/ | For matching |
Sensitive | Sensitive('secret') | Hidden in logs and reports |
Timestamp, Timespan | Timestamp() | Dates and durations |
Type Annotations
Puppet has a type system. You can say what kind of value a variable or class parameter must hold. Wrong data is rejected early with a clear error.
These annotations are most useful on class parameters (next lesson). You can check types yourself:
Facts: Information About the Node
Facter collects facts before every run. They are available as the $facts hash. Use the hash form, not old top-level variables.
| Common fact | Meaning |
|---|---|
$facts['os']['family'] | OS family: Debian, RedHat, Suse, windows |
$facts['os']['name'] | Distribution: Ubuntu, Rocky, Windows |
$facts['os']['release']['major'] | Major version number |
$facts['networking']['fqdn'] | Fully qualified domain name |
$facts['networking']['ip'] | Primary IP address |
$facts['kernel'] | Linux, windows |
$facts['processors']['count'] | CPU count |
$facts['is_virtual'], $facts['virtual'] | Running in a VM, and the platform |
Trusted Facts
Normal facts are reported by the node, so a compromised or misconfigured node could lie. Trusted facts come from the node's certificate and are verified by the server, so use them for security decisions.
Operators
| Group | Operators |
|---|---|
| Arithmetic | + - * / % |
| Comparison | == != < > <= >= (string comparison ignores case) |
| Boolean | and or ! |
| Match | =~ (matches), !~ (does not match) |
| Membership | in (value in array, key in hash, substring in string) |
| Array and hash | + (join), - (remove), << (append), * (splat) |
Useful Built-in Functions
| Function | Example | Result |
|---|---|---|
notice, warning, err | notice('hi') | Writes a log line (use for debugging) |
fail | fail('unsupported OS') | Stops compilation with an error |
length | 'puppet'.length | 6 |
upcase, downcase, capitalize | 'web'.upcase | WEB |
join, split | ['a','b'].join('-') | a-b |
regsubst | regsubst('web01', '\d+', 'NN') | webNN |
sprintf | sprintf('%05d', 42) | 00042 |
merge | merge($h1, $h2) | Combined hash |
keys, values | $h.keys | Array of keys |
lookup | lookup('app::port') | Reads data from Hiera |
file, template, epp | file('mod/x.txt') | Read or render files |
sha256, md5 | sha256('x') | Hash value |
Functions can be called as function(value) or as a method on a value: $name.upcase. Both are valid. Use the style that reads better.
Scope: Where Variables Are Visible
- Top scope: variables defined outside classes, reachable everywhere as
$::name(rarely used) - Node scope: inside a node block
- Class scope: variables inside a class belong to the class. Other classes read them as
$classname::variable - Local scope: inside lambdas and defined types
Sensitive Data
Debugging Tips
notice()prints values at compile time (on the server in a server setup)puppet apply -e 'notice(1+2)'is the quickest scratchpadpuppet lookupandfactershow data used by the codepuppet parser validate file.ppchecks syntax without running
Practice Tasks
- Print five different facts of your lab machine with
notice - Create a hash of three web servers with ports and print the port of the second one
- Add type annotations to a variable with
EnumandInteger[1,65535]and trigger an error with a wrong value - Use
regsubstto turn a hostname likeweb07.example.comintoweb07 - Wrap a password in
Sensitiveand confirm it is hidden in output
Conclusion
Puppet variables are set once, data has strong types, and the $facts hash describes each node. Use operators and functions to build values, use trusted facts for security, and Sensitive for secrets.

