0

Suppose, the following Puppet code:

define apache::base($pkgver = '2.4.10') {
    $apache_ver = $pkgver
    ...
}
define apache::vhost($instance) {
    ...
    $apache_ver = getvar(......)
}

apache::base{ "static-files":}
apache::base{ "dynamic": pkgver => '2.4.8' }
apache::vhost{ "static.example.com": instance => "static-files"}

How can the code in apache::vhost refer to $pkgver (parameter) or $apache_ver (variable) in the corresponding apache::base?

Our stdlib is too old (and our Puppet is still 2.7.x) and does not have getparam(). getvar() ought to be able to do it -- but how? What is the full name of the variable in this case?

I tried getvar("apache::base[$instance]::apache_ver") and getvar("apache::base::$instance::apache_ver") to no avail -- getvar returns an empty string... What's the right method?

Mikhail T.
  • 2,338
  • 1
  • 24
  • 55

1 Answers1

0

You don't.

To get at such a value, you would have to refer to an instance of your defined type, e.g.

Apache::Vhost['main-site']::server_alias

Such a thing is not implemented, though.

You need to restructure your model. In your module, apache::vhost cannot be independent of apache::base. Instead, you will want to hand a resource hash to apache::base so that it can declare the vhosts itself.

define apache::base($pkgver = '2.4.10', $vhosts = {}) {
  $apache_ver = $pkgver
  ...
  create_resources('apache::vhost', $vhosts, { instance => $title })
}

And use it like

apache::base{ "static-files":
  vhosts => {
    "static.example.com" => { 
      # attributes for Apache::Vhost["static.example.com"] go here
    },
  }
}
Felix Frank
  • 3,093
  • 1
  • 16
  • 22
  • The "such a thing is not implemented" is the most valuable part of your answer, Felix. Thank you... But are you sure? Is not `getvar()` supposed to do just that -- if invoked properly? – Mikhail T. Jun 24 '15 at 18:28
  • To be more specific: The Puppet language has no construct that allows this kind of cross-reference. There are parser functions that *will* allow you to [inspect the catalog during build](https://github.com/puppetlabs/puppetlabs-stdlib/blob/f820bb156038f638d8e488286d0c2b92c5636925/lib/puppet/parser/functions/ensure_resource.rb#L38), but using them is dangerous because their result is evaluation order dependent (i.e., they are not reliable). – Felix Frank Jun 25 '15 at 09:42
  • Thanks for the warning -- I will ensure the order base->vhost. But, still, what is the proper way to refer to a particular instance of apache::base with `getvar()`? – Mikhail T. Jun 26 '15 at 12:43
  • `getvar` cannot as far as I know. You would need to build a custom function, basically a variation on [defined_with_params](https://github.com/puppetlabs/puppetlabs-stdlib/blob/master/lib/puppet/parser/functions/defined_with_params.rb) but again: **This is very prone for latent errors and should be avoided if possible in any way**. – Felix Frank Jun 26 '15 at 17:59
  • Wait, is not that the very _purpose_ of `getvar()`?.. – Mikhail T. Jun 27 '15 at 19:47
  • No. `getvar` is basically just a wrapper for the `lookupvar` scope method. It cannot access a define's parameters or declared variables because defined types don't spawn namespaces for their instances. Looking up *class* parameters is no problem, but with defines, it won't work. – Felix Frank Jun 27 '15 at 21:03