Write a function to puupet

24 views
Skip to first unread message

shlo....@gmail.com

unread,
Jan 15, 2014, 5:28:41 AM1/15/14
to puppet...@googlegroups.com


Hi,
I want to write a function for Puppet that check if package already defined, I saw in this group to do:

define safepackage ( $ensure = present ) {
        if !defined(Package[$title]) {
                package { $title: ensure => $ensure }
        }
}

In which file I need put this function so all other modules can access it?  because I'm getting 'unknown function...' error.



jcbollinger

unread,
Jan 15, 2014, 9:10:53 AM1/15/14
to puppet...@googlegroups.com

That's not a function, it's a defined type.  You declare instances just as you would declare instances of a built-in type, such as Package itself.  You cannot call it as if it were a function.  You really ought to put it into a module, though.

With that said, what you propose to do is one of the persistent bad ideas in the Puppet universe.  Using such a definition to allow multiple points of declaration of a given resource (only one of which will be effective) makes your manifest set harder to maintain and opens it to subtle bugs because you must keep all declarations of the target resource synchronized.  If you don't, then your manifest set thereby has an internal inconsistency that you prevent Puppet from diagnosing.  All manner of strangeness may then ensue.  Moreover, the approach is only effective for a given resource if you apply it to every declaration of that resource.

The best way to avoid multiple-declaration errors is to factor out multiple declarations of any given resource into a single class that the other erstwhile declaration points will rely on.  That is usually fairly natural.  It may involve modifying third-party modules, but so does any approach based on the defined() function or similar.


John

Reply all
Reply to author
Forward
0 new messages