The Puppet Labs Issue Tracker has Moved: https://tickets.puppetlabs.com

Bug #3490

Defined() requires the class to exist when used in an if statement

Added by Ashley Penney about 4 years ago. Updated about 2 years ago.

Status:ClosedStart date:03/31/2010
Priority:LowDue date:
Assignee:-% Done:

0%

Category:functions
Target version:-
Affected Puppet version:0.25.4 Branch:
Keywords:defined function

We've Moved!

Ticket tracking is now hosted in JIRA: https://tickets.puppetlabs.com

This issue is currently not available for export. If you are experiencing the issue described below, please file a new ticket in JIRA. Once a new ticket has been created, please add a link to it that points back to this Redmine ticket.


Description

This may not be a bug, but it should be documented perhaps as it was surprising to me. While doing some testing if a new module I pushed the following snippet to my production modules:

if defined(Class[“splunk4::client”]) { stuff here }

This then failed when nodes checked in with:

err: Could not retrieve catalog from remote server: Error 400 on SERVER: Could not find class splunk4::client at /etc/puppet/modules/production/rhythmyx/manifests/init.pp:20 on node hlstestper1.law.harvard.edu

because the splunk4 module wasn’t in production. Simply moving it into the production environment (and including it nowhere) was enough to get it working which was unexpected. I would expect in an if statment that it would simply ignore any errors from defined and skip the block, not crash and burn.

History

#1 Updated by Trevor Vaughan about 4 years ago

I’ve also run into this and also believe that the expected behaviour is to have defined simply return ‘false’ if the item does not exist at all.

Looking at the code for ‘defined’, it seems that a couple of catches would do what is required to make this work as expected.

#2 Updated by James Turnbull about 4 years ago

  • Status changed from Unreviewed to Needs Decision
  • Assignee set to Luke Kanies

Seems reasonable to me if you agree Luke?

#3 Updated by Luke Kanies about 4 years ago

  • Status changed from Needs Decision to Accepted
  • Assignee deleted (Luke Kanies)

Definitely a bug.

#4 Updated by Jordan Sissel about 2 years ago

i cannot reproduce this on puppet master (0f8747f37650523d3deca75f5091feeef1b238cb) and

snapple(~) !1! % puppet --version
2.7.9
snapple(~) % puppet apply -e 'if defined(Class["splunk4::client"]) { notice("Found") } else { notice("Not found") }'
notice: Scope(Class[main]): Not found
notice: Finished catalog run in 0.03 seconds

#5 Updated by Jordan Sissel about 2 years ago

  • Status changed from Accepted to Closed

Confirmed. This was fixed in 2.6.0 (broken in previous release, 0.25.6)

% git checkout 0.25.6; RUBYLIB=lib ruby bin/puppet apply -e 'notice(defined(Class["hello::world"])) if defined(Class["hello::world"]) { notice("Found") } else { notice("Not found") }'
HEAD is now at e34a531... Updated CHANGELOG for 0.25.6
Could not find class hello::world at line 1 on node snapple.home

% git checkout 2.6.0; RUBYLIB=lib ruby bin/puppet apply -e 'notice(defined(Class["hello::world"])) if defined(Class["hello::world"]) { notice("Found") } else { notice("Not found") }'
HEAD is now at db0b30d... Updated CHANGELOG for 2.6.0
notice: Scope(Class[main]): false
notice: Scope(Class[main]): Not found

Also available in: Atom PDF