Blog

Answers about Puppet

DevOps, Automation

Universe and Everything

Need Puppet help?

Contact Alessandro Franceschi / example42
for direct expert help on Puppet.
If solution is quick, it’s free. No obligations.

Tip of the Week 9 - Building your own Puppet 4 Data Types

Update: Tip of the Week 72 - Puppet [custom] data types

Puppet comes with a set of core data types like Integer, Float, String, Boolean.

But what if you have special needs? How to reimplement the stdlib functions like validate_absolute_path() or validate_ipv4()?

Puppet allows you to place your own build data types into a modules type directory:

modulepath/
\-  firewall/
     |- manifests/
     | \- init.pp
     |- lib/
     |  |- facter
     |  \- puppet
     |- facts.d
     \- types
        \- ipv4address.pp

Inside the types directory you can specify a new data type. This new data type is available in your module namespace:

# firewall/types/ipv4address.pp
type firewall::ipv4address = Pattern[/^((([0-9](?!\d)|[1-9][0-9](?!\d)|1[0-9]{2}(?!\d)|2[0-4][0-9](?!\d)|25[0-5](?!\d))[.]){3}([0-9](?!\d)|[1-9][0-9](?!\d)|1[0-9]{2}(?!\d)|2[0-4][0-9](?!\d)|25[0-5](?!\d)))(\/((([0-9](?!\d)|[1-9][0-9](?!\d)|1[0-9]{2}(?!\d)|2[0-4][0-9](?!\d)|25[0-5](?!\d))[.]){3}([0-9](?!\d)|[1-9][0-9](?!\d)|1[0-9]{2}(?!\d)|2[0-4][0-9](?!\d)|25[0-5](?!\d))|[0-9]+))?$/]

You can use any data type for building new ones.

Some combined data types are already available in puppetlabs-stdlib or voxpupuli-tea.

Martin Alfke