Ask Your Question

Can't reference variable in superclass

asked 2015-09-03 16:43:59 -0600

syphrix gravatar image

For example, I have some code very similar to this in a module I'm building

class doolittle {
  $location1 = '/path/to/location1'
  $location2 = '/path/to/location2'

class doolittle::install {
  file { $::location1: ensure => directory }
  file { $::location2: ensure => directory }

But when I run it, it gives me an error

Missing title. The title expression resulted in undef at /etc/puppetlabs/code...

What's the correct way to reference the variables from the superclass in situations like this?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2015-09-03 16:48:21 -0600

ramindk gravatar image

Your variables aren't in top scope when they are declared, they are in the scope of the class that declared them. Try the following.

class doolittle::install {
  file { $::doolittle::location1: ensure => directory }
  file { $::doolittle::location2: ensure => directory }
edit flag offensive delete link more


Ah yes, that definitely fixes my issue. Since that is a rather long way to reference variables that may be passed in as arguments, is there a way to include them in the local scope? Sorry for the extra question, I'm rather new to this Puppet business :)

syphrix gravatar imagesyphrix ( 2015-09-03 17:18:43 -0600 )edit

I think I figured it out. inherits is the keyword I need class doolittle::install inherits doolittle { notify { $location1: } notify { $location2: } } It was confusing to me because I thought that's what the 'doolittle::' prefix to my install class was already doing for me

syphrix gravatar imagesyphrix ( 2015-09-03 17:33:12 -0600 )edit

Yep that works, but watch the inheritance. It's an easy way to get yourself into trouble. If you do it once per module, you'll probably be fine. More than that starts to get confusing and a class in Puppet isn't a 'class' as it might be in a programming language.

ramindk gravatar imageramindk ( 2015-09-03 18:51:37 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2015-09-03 16:43:59 -0600

Seen: 287 times

Last updated: Sep 03 '15