Puppet resource scoping issue

41 views
Skip to first unread message

Boris

unread,
Jan 17, 2013, 2:03:03 AM1/17/13
to puppet...@googlegroups.com
I have a service resource in class 'foo', and i want to make a relationship with this resource from class 'bar'. Both 'foo' and 'bar' classes are included in my node definition.
From puppet docs i learned that dynamic scoping is still available for resources, but what happens in reality, is from-time-to-time error "Could not find dependent Service".

Can someone, please, help to explain this behavior?

jcbollinger

unread,
Jan 17, 2013, 9:45:48 AM1/17/13
to puppet...@googlegroups.com


Puppet resources never had dynamic scope.  Instead, they have global scope once they are parsed.

Wherever in your manifests you want to refer to a class or resource declared elsewhere, it is essential to ensure that that external class or resource has already been parsed.  Provided that you are not using parametrized classes, or that you do not declare your classes with explicit parameter values, the best way to ensure a correct parse order is to 'include' the class declaring (or constituting) the target resource.  So:

modules/mymodule/manifests/foo.pp:
------------------------------------------------------
class mymodule::foo {
  service { 'foo_service': }
}

modules/mymodule/manifests/bar.pp:
------------------------------------------------------
class mymodule::bar {
  include 'mymodule::foo'
  some_resource { 'bar_resource':
    require => Service[ 'foo_service' ]
  }
}


Note also that there is a growing sentiment that it is better style to avoid cross-class relationships.  Advocates of that style would instruct you that instead of establishing a relationship directly with a resource declared in a different class, you should establish the relationship with the class that declares the resource.  I am not yet fully persuaded, but there are good arguments in support of that position.  To following that style rule, class bar would be written like this:

modules/mymodule/manifests/bar.pp:
------------------------------------------------------
class mymodule::bar {
  include 'mymodule::foo'
  some_resource { 'bar_resource':
    require => Class[ 'mymodule::foo' ]
  }
}



John

Boris

unread,
Jan 17, 2013, 10:20:40 AM1/17/13
to puppet...@googlegroups.com
Thanks a lot for the explanation! 
It became much clearer to me.
Reply all
Reply to author
Forward
0 new messages