Ask Your Question

what is difference between class {'::XYZ'} and class {'XYZ'}

asked 2016-12-12 13:17:12 -0600

linux168 gravatar image

What is difference between class {'::XYZ': } and class{'XYZ': }

The 2nd question I have is 
   # cat foo.pp
   if ! defined(Class['XYZ']) { ...}
   If I have a module 'XYZ' in the modulepath but not in in foo.pp , does the defined(Class['XYZ']) return true or false?
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2016-12-12 14:43:14 -0600

smarlow gravatar image

The difference between class { '::XYZ': } and class { 'XYZ': } is that the first explicitly says that it's looking for the class XYZ in the top scope. Most of the time this isn't necessary to do. However, in previous versions of Puppet I ran into issues where the name of a profile that I was using shadowed the name of a component module. Here's an example:

# modules/profile/manifests/xyz.pp
class profile::xyz (
) {
  # Trying to declare the base class in the xyz module;
  # Actually declares profile::xyz
  # class { 'xyz': }

  # Explicitly specifying top-scope to make sure it knows what class I mean
  class { '::xyz': }

Rather than declaring the xyz class it would try to declare profile::xyz instead and throw an error. In that case I would use class { '::xyz': } to disambiguate which class I was actually talking about. This may have changed in more recent versions of Puppet, but I haven't yet verified that.

With regards to your second question: defined() will return true if the class has been defined in the catalog already at the time of execution. Being in the modulepath does not mean that the class will be defined automatically. Here's an example:

[root@mybox ~]# puppet apply -e '$v = defined(Class['ntp']); notify { "NTP: ${v}": }' --noop
Notice: Compiled catalog for mybox in environment production in 0.11 seconds
Notice: /Stage[main]/Main/Notify[NTP: false]/message: current_value absent, should be NTP: false (noop)

[root@mybox ~]# puppet apply -e 'include ntp; $v = defined(Class['ntp']); notify { "NTP: ${v}": }' --noop
Notice: Compiled catalog for mybox in environment production in 1.17 seconds
Notice: /Stage[main]/Main/Notify[NTP: true]/message: current_value absent, should be NTP: true (noop)

However, I would strongly recommend against using defined(). It depends on the order in which your manifests are executed, which can be tricky to nail down. It also makes it possible for your execution flow to be dependent on what classes are declared in external modules. Usually you can replace defined() with a parameter like $manage_xyz which allows you to control things a bit more reliably and explicitly.

edit flag offensive delete link more


Thanks foe the answers. They are very helpful :) Can you give me an example of 'replacing defined() with a parameter'! For example, how do I replace ' if !defined(Class['XYZ']) { include XYZ }'

linux168 gravatar imagelinux168 ( 2016-12-12 17:13:48 -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: 2016-12-12 13:15:30 -0600

Seen: 72 times

Last updated: Dec 12 '16