Quantcast
Channel: Unpunctual Programmer » Devops
Viewing all articles
Browse latest Browse all 8

Fun with Puppet’s defined function.

$
0
0

I just spent an embarrassing amount of time trying to figure out why some resources in one of my puppet class where not being executed. All of these were inside of an if branch that was checking if another class was defined. It was defined (wouldn’t be much of a blog post if that wasn’t the case), there were not errors, it just wasn’t happening. Turns out that defined does not work the way I thought it did. From Puppet’s documentation (https://docs.puppetlabs.com/references/latest/function.html#defined):

Checking whether a given resource has been declared is, unfortunately, dependent on the parse order of the configuration

Because defined happens during the parse step and not another step the order in which your resources are declared matters. To give you a concrete example, I have two classes, php and imagemagick. The imagemagick class installs the imagick php extension for you if php is defined. How nice of it, right? This has always worked perfectly. Until my most recent manifest where I had something like:

class{'imagemagick': }
class{'php':
  before => Class['imagemagick'],
}

Which looks good. Php has to happen before imagemagick so it should be defined. But, because imagemagick is parsed before php, php isn’t actually defined for imagemagick, so nothing inside of my if was run during the apply. To make this actually work it needed to like like:

class{'php':
before => Class['imagemagick'],
}
class{'imagemagick': }

And now it works. Ridiculous. Hope this saves someone some time.



Viewing all articles
Browse latest Browse all 8

Trending Articles