Read more

When to use a function over a defined in Puppet

Moritz Kraus
December 15, 2022Software engineer at makandra GmbH

In Puppet, there are some differences between defined and function types when it comes to code encapsulation. In most cases, a defined type is best, but there are some situations where a function is more appropriate.

Illustration online protection

Rails Long Term Support

Rails LTS provides security patches for old versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2)

  • Prevents you from data breaches and liability risks
  • Upgrade at your own pace
  • Works with modern Rubies
Read more Show archive.org snapshot

Use a defined type when:

  • You need to retrieve the resource or its parameter via puppetdb_query().
  • The containing code creates real resources.
  • You need a specific wrapper for the code.

Use a function when:

  • You need to return data from the code.
  • You don't need to specify a resource name.
  • You need a general wrapper for the code.

Additionally, functions are useful for transforming data, such as combining data from multiple sources or converting data from one format to another. Functions can also help to encapsulate complex or repetitive code, making your Puppet code easier to read and maintain.

Abstraction can also lead to entanglement, where different parts of the code become tightly coupled and difficult to modify or maintain.

Posted by Moritz Kraus to makandra Operations (2022-12-15 17:03)