FAQ
Hi everybody,

I'm a new puppet user, and I have a question about defined types and
relationships.

I want a defined type to be part of a module.
It manages optional configuration files, so the user can decide to declare
it one or more times or not to declare it at all.
In the case the defined type is declared, I want the contained resources to
be in relationship with other resources in my main module class; in
particular, I want the resources in the defined type to be managed *after*
package and *before* service.

Two ways come to mind:
1) embed the necessary require/before (and/or subscribe/notify) inside the
the defined type's resources:

# == Define: software::mydefinedtype
define software::mydefinedtype {
   file { "/path/to/${name}":
     [...]
     require => Package['software'],
     before => Service['software'],
   }
}

2) rely on dependency chains and a resource collector in the main module
class (and of course on the containment properties of defined types):

# == Class: software
class software {
   [...]
   Package['software'] -> Software::Mydefinedtype <| |> ->
Service['software']
}

What is, in your opinion, the best method?
I've tried both, and both apparently work with no issues.

One doubt about method 2:
at http://docs.puppetlabs.com/puppet/3/reference/lang_relationships.html, I
can read:
"If one of the resources in a relationship is never declared, compilation
will fail with one of the following errors [...]"
I suppose that this doesn't apply in case of resources chained through
resource collectors, does it? I'm asking because, even without declarations
of instances of the defined type, I got no such errors.

Thanks a lot for your help!

--
You received this message because you are subscribed to the Google Groups "Puppet Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users+unsubscribe@googlegroups.com.
To post to this group, send email to puppet-users@googlegroups.com.
Visit this group at http://groups.google.com/group/puppet-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Jcbollinger at May 30, 2013 at 1:13 pm

    On Wednesday, May 29, 2013 10:29:22 AM UTC-5, Fabio Sangiovanni wrote:
    Hi everybody,

    I'm a new puppet user, and I have a question about defined types and
    relationships.

    I want a defined type to be part of a module.
    It manages optional configuration files, so the user can decide to declare
    it one or more times or not to declare it at all.
    In the case the defined type is declared, I want the contained resources
    to be in relationship with other resources in my main module class; in
    particular, I want the resources in the defined type to be managed *after*
    package and *before* service.

    Two ways come to mind:
    1) embed the necessary require/before (and/or subscribe/notify) inside the
    the defined type's resources:

    # == Define: software::mydefinedtype
    define software::mydefinedtype {
    file { "/path/to/${name}":
    [...]
    require => Package['software'],
    before => Service['software'],
    }
    }

    2) rely on dependency chains and a resource collector in the main module
    class (and of course on the containment properties of defined types):

    # == Class: software
    class software {
    [...]
    Package['software'] -> Software::Mydefinedtype <| |> ->
    Service['software']
    }

    What is, in your opinion, the best method?
    I've tried both, and both apparently work with no issues.

    For this particular pattern of requirements, I strongly prefer method 1.
    It keeps everything associated directly with the defined type together in
    one place. I would suggest one tweak, however: the defined type should
    explicitly 'include' class 'software' if it refers to resources declared by
    that class. For example:

    define software::mydefinedtype {
       include 'software'
       file { "/path/to/${name}":
         [...]
         require => Package['software'],
         before => Service['software'],
       }
    }

    Note also that if you prefer the chain operators to the metaparameters, you
    can use the former in your defined type:

    define software::mydefinedtype {
       include 'software'
       file { "/path/to/${name}":
         [...]
       }
         Package['software'] -> File["/path/to/${name}"] -> Service['software']
    }

    Indeed, I think you can even write it like this:

    define software::mydefinedtype {
       include 'software'
       Package['software'] ->
       file { "/path/to/${name}":
         [...]
       } ->
       Service['software']
    }


    One doubt about method 2:
    at http://docs.puppetlabs.com/puppet/3/reference/lang_relationships.html,
    I can read:
    "If one of the resources in a relationship is never declared, compilation
    will fail with one of the following errors [...]"
    I suppose that this doesn't apply in case of resources chained through
    resource collectors, does it? I'm asking because, even without declarations
    of instances of the defined type, I got no such errors.
    Personally, I am uncomfortable with relying on undocumented behavior. It
    may change without warning from one version of the software to another.
    Even if that does not bother you, in this particular case you should also
    verify that the transitively implied relationship between
    Package['software'] and Service['software'] is still present when no
    instances of Software::Mydefinedtype are declared. You can make puppet
    generate a relationship graph to check.


    John

    --
    You received this message because you are subscribed to the Google Groups "Puppet Users" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users+unsubscribe@googlegroups.com.
    To post to this group, send email to puppet-users@googlegroups.com.
    Visit this group at http://groups.google.com/group/puppet-users?hl=en.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Fabio Sangiovanni at Jun 3, 2013 at 7:52 am
    Hi, thanks for your answer.
    On Thursday, May 30, 2013 3:13:45 PM UTC+2, jcbollinger wrote:


    For this particular pattern of requirements, I strongly prefer method 1.
    It keeps everything associated directly with the defined type together in
    one place. I would suggest one tweak, however: the defined type should
    explicitly 'include' class 'software' if it refers to resources declared by
    that class. For example:

    define software::mydefinedtype {
    include 'software'
    file { "/path/to/${name}":
    [...]
    require => Package['software'],
    before => Service['software'],
    }
    }
    Yes, that's what I actually did in the end. And I've also used the include,
    as you suggested (I didn't post my whole code in the first post, just the
    relevant part).

    Note also that if you prefer the chain operators to the metaparameters, you
    can use the former in your defined type:

    define software::mydefinedtype {
    include 'software'
    file { "/path/to/${name}":
    [...]
    }
    Package['software'] -> File["/path/to/${name}"] -> Service['software']
    }

    Indeed, I think you can even write it like this:

    define software::mydefinedtype {
    include 'software'
    Package['software'] ->
    file { "/path/to/${name}":
    [...]
    } ->
    Service['software']
    }
    Clever! Thanks :)

    One doubt about method 2:
    at http://docs.puppetlabs.com/puppet/3/reference/lang_relationships.html,
    I can read:
    "If one of the resources in a relationship is never declared, compilation
    will fail with one of the following errors [...]"
    I suppose that this doesn't apply in case of resources chained through
    resource collectors, does it? I'm asking because, even without declarations
    of instances of the defined type, I got no such errors.
    Personally, I am uncomfortable with relying on undocumented behavior. It
    may change without warning from one version of the software to another.
    Even if that does not bother you, in this particular case you should also
    verify that the transitively implied relationship between
    Package['software'] and Service['software'] is still present when no
    instances of Software::Mydefinedtype are declared. You can make puppet
    generate a relationship graph to check.
    I avoided the issue too, I'll try to generate the graphs as soon as I have
    some spare time.
    John
    Thanks again,

    Fabio

    --
    You received this message because you are subscribed to the Google Groups "Puppet Users" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users+unsubscribe@googlegroups.com.
    To post to this group, send email to puppet-users@googlegroups.com.
    Visit this group at http://groups.google.com/group/puppet-users?hl=en.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Nick Fagerlund at Jun 3, 2013 at 8:33 pm
    There's a third way, too. In the defined type's definition:

    define software::mydefinedtype {
       include software
       Package['software'] -> Software::Mydefinedtype[$title]
       ...
       ...etc.
    }

    That creates a relationship between the package and every resource in each
    instance of the defined type. Every instance you declare will create its
    own relationships.

    This approach has the concision of the second example you were considering,
    but works more like the first example.

    Also:

    One doubt about method 2:
    at http://docs.puppetlabs.com/puppet/3/reference/lang_relationships.html,
    I can read:
    "If one of the resources in a relationship is never declared, compilation
    will fail with one of the following errors [...]"
    I suppose that this doesn't apply in case of resources chained through
    resource collectors, does it? I'm asking because, even without declarations
    of instances of the defined type, I got no such errors.
    Yeah, with a collector that doesn't catch anything, a chaining statement
    won't create any relationships, and shouldn't blow up.

    N

    --
    You received this message because you are subscribed to the Google Groups "Puppet Users" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users+unsubscribe@googlegroups.com.
    To post to this group, send email to puppet-users@googlegroups.com.
    Visit this group at http://groups.google.com/group/puppet-users?hl=en.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppuppet-users @
categoriespuppet
postedMay 29, '13 at 6:43p
activeJun 3, '13 at 8:33p
posts4
users3
websitepuppetlabs.com

People

Translate

site design / logo © 2021 Grokbase