puppet

Default parameters that rely on other parameters


I wrote a module for chruby, the Ruby version manager. This works fine with Puppet v3, but I just started using Puppet v4 and the $version param does not get interpolated in the $source_url string.

class chruby(
  $version,
  $source_url = "https://github.com/postmodern/chruby/archive/v${version}.tar.gz",
) {

I'll always want a version passed, and I may want a source url passed though usually not - have the rules changed that this is no longer allowed, and how can I get this to work with v4? I tried this:

  unless $source_url {
    $source_url = "https://github.com/postmodern/chruby/archive/v${version}.tar.gz"
  }

In the class body but it also doesn't interpolate. I've checked there is a $version using notice. I can't find how to do this from the docs:

I have started using Hiera for some things and understand this may remove the need for defaults, but I've just started using it this last week so I'm not clear on that yet, but still would like to understand why this has changed from v3 to v4.


Solution

  • Use another variable:

    class chruby(
      $version,
      $source_url = undef,
    ) {
      $actual_source_url = $source_url ? {
        undef   => "https://github.com/postmodern/chruby/archive/v${version}.tar.gz",
        default => $source_url,
      }
      notice($actual_source_url)
    }