Bug #2309

Defined resources are not evaluated immediately which can cause ordering problems

Added by Ohad Levy about 1 year ago. Updated 6 months ago.

Status:Accepted Start:05/29/2009
Priority:Normal Due date:
Assigned to:- % Done:

0%

Category:language
Target version:Statler
Affected version:0.24.8 Branch:
Keywords:
Votes: 0

Description

Because defined resources are queued as they’re defined, and only evaluated when all other work is done, we can see ordering issues. In the example below, the two classes are evaluated to the level of the defined resource, and then stopped. This results in the variable modification in the defined resource not getting called until after the template is evaluated.

The correct behaviour should see the defined resource getting evaluated as soon as it is added to the catalog, rather than queueing it for later.

original description:

it seems that templates ignore variable scopes, consider this code:


node default {
    include a
    include b
}

class a {
    $dirs = ""
    dir{["/tmp","/tmp/a","/tmp/b"]: before => File["/tmp/dirs"]}
}
class b {
    file{"/tmp/dirs": content => $a::dirs}
    #file{"/tmp/dirs": content => template("/tmp/dirs.erb")}
}

define dir() {
    $a::dirs += " $name"
    file {$name: ensure => directory }
}

using template the output of the file is “”, but when dumping the variable directly, the content is correct.

History

Updated by Luke Kanies about 1 year ago

  • Status changed from Unreviewed to Rejected

I don’t know what your template has in it, but I’m pretty sure templates can’t look up scoped variables.

Instead, copy the variable into your local scope:

$dirs = $a::dirs

and use that in your template.

Updated by Ohad Levy about 1 year ago

  • Status changed from Rejected to Re-opened

The contennt of the template is:

<%= scope.lookupvar("a::dirs") %>

I’ve also tried your solution before, which doesn’t work as well.

I think that the main problem is here: (which got me to the conclusion that the order is wrong) running with template – you can see that the template is processed first.

puppet -d test.pp
debug: Scope(Class[b]): Retrieving template /tmp/dirs.erb
debug: template[/tmp/dirs.erb]: Bound template variables for /tmp/dirs.erb in 0.00 seconds
debug: template[/tmp/dirs.erb]: Interpolated template /tmp/dirs.erb in 0.00 seconds
debug: Defining before on Dir[/tmp]
debug: Defining before on Dir[/tmp/a]
debug: Defining before on Dir[/tmp/b]
debug: Creating default schedules
debug: Finishing transaction 69822889213800 with 0 changes
debug: //Node[default]/a/Dir[/tmp/a]/before: requires File[/tmp/dirs]
debug: //Node[default]/a/Dir[/tmp/b]/File[/tmp/b]/before: requires File[/tmp/dirs]
debug: //Node[default]/a/Dir[/tmp]/before: requires File[/tmp/dirs]
debug: //Node[default]/a/Dir[/tmp/a]/File[/tmp/a]/before: requires File[/tmp/dirs]
debug: //Node[default]/a/Dir[/tmp]/File[/tmp]/before: requires File[/tmp/dirs]
debug: //Node[default]/a/Dir[/tmp/b]/before: requires File[/tmp/dirs]
debug: //Node[default]/a/Dir[/tmp/b]/File[/tmp/b]: Autorequiring File[/tmp]
debug: //Node[default]/a/Dir[/tmp/a]/File[/tmp/a]: Autorequiring File[/tmp]
debug: //Node[default]/b/File[/tmp/dirs]/checksum: Initializing checksum hash
debug: //Node[default]/b/File[/tmp/dirs]: Creating checksum {md5}68b329da9893e34099c7d8ad5cb9c940
debug: Finishing transaction 69822890682820 with 0 changes

running with no template

puppet -d test.pp
debug: Defining before on Dir[/tmp/b]
debug: Defining before on Dir[/tmp/a]
debug: Defining before on Dir[/tmp]
debug: Creating default schedules
debug: Finishing transaction 70189412780520 with 0 changes
debug: //Node[default]/a/Dir[/tmp/a]/before: requires File[/tmp/dirs]
debug: //Node[default]/a/Dir[/tmp]/before: requires File[/tmp/dirs]
debug: //Node[default]/a/Dir[/tmp/b]/File[/tmp/b]/before: requires File[/tmp/dirs]
debug: //Node[default]/a/Dir[/tmp/a]/File[/tmp/a]/before: requires File[/tmp/dirs]
debug: //Node[default]/a/Dir[/tmp]/File[/tmp]/before: requires File[/tmp/dirs]
debug: //Node[default]/a/Dir[/tmp/b]/before: requires File[/tmp/dirs]
debug: //Node[default]/a/Dir[/tmp/b]/File[/tmp/b]: Autorequiring File[/tmp]
debug: //Node[default]/a/Dir[/tmp/a]/File[/tmp/a]: Autorequiring File[/tmp]
debug: //Node[default]/b/File[/tmp/dirs]/checksum: Initializing checksum hash
debug: //Node[default]/b/File[/tmp/dirs]: Creating checksum {md5}68b329da9893e34099c7d8ad5cb9c940
debug: //Node[default]/b/File[/tmp/dirs]: Changing content
debug: //Node[default]/b/File[/tmp/dirs]: 1 change(s)
notice: //Node[default]/b/File[/tmp/dirs]: Filebucketed to  with sum 68b329da9893e34099c7d8ad5cb9c940
debug: //Node[default]/b/File[/tmp/dirs]/checksum: Replacing /tmp/dirs checksum {md5}68b329da9893e34099c7d8ad5cb9c940 with {md5}dd50ac53127aa2f3578835acc1415712
notice: //Node[default]/b/File[/tmp/dirs]/content: content changed '{md5}68b329da9893e34099c7d8ad5cb9c940' to '{md5}dd50ac53127aa2f3578835acc1415712'
debug: Finishing transaction 70189414240760 with 1 changes

another observation:

when changing the variable from a string to array (e.g. $dirs = [“”] instead of $dirs = “”) nothing works at all.

Updated by Luke Kanies about 1 year ago

  • Subject changed from templates are processed too early in the compliation to Defined resources are not evaluated immediately which can cause ordering problems
  • Category set to language
  • Status changed from Re-opened to Accepted
  • Target version set to 2.6.0

Updated by James Turnbull 6 months ago

  • Target version changed from 2.6.0 to Statler

Also available in: Atom PDF