Read more

When to use a function over a defined in Puppet

Avatar
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 web development

Do you need DevOps-experts?

Your development team has a full backlog? No time for infrastructure architecture? Our DevOps team is ready to support you!

  • We build reliable cloud solutions with Infrastructure as code
  • We are experts in security, Linux and databases
  • We support your dev team to perform
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)