Ask Your Question
1

Can't reference variable in superclass

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

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
1

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

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

Comments

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 -0500 )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 -0500 )edit
1

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 -0500 )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

Stats

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

Seen: 210 times

Last updated: Sep 03 '15