Re: [Puppet Users] DRY duplicated manifest code
On 23/03/16 21:04, Hunter Haugen wrote: Given the resource you want to apply this pattern to, it can be turned into a one-liner with a collector: file { '/tmp/something': ensure => file, } File['/tmp/something'] ~> Service <| title == 'apache2' |> This means that if there is a service with a title of apache2 EVER added to the catalog, it'll be refreshed on file changes. If the service doesn't exist, then the dependency does nothing. Now, this isn't exactly what you asked since you wanted the variable $services_to_notify and didn't say what you're going to do with it, but I assume this is what you want? Because collectors are not parse-order specific, you can't do variable assignments like $services_to_notify = Service <| title == 'apache2' |> (because variables are evaluated in parse order and collectors are not). If you really want to make a function that searches the catalog and returns references, it can be done with something like `scope.catalog.resource('Service[apache2]')` inside the function I believe, though that may not be the exact call. That route will make you discover things that you cannot do. Functions are called in "evaluation order", and all defaults, collections, overrides have not yet been applied to resources at the point where a function is called. IMO you will be better off by using collectors or rethink the design. - henrik -- Visit my Blog "Puppet on the Edge" http://puppet-on-the-edge.blogspot.se/ -- 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+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/56F307E2.2090903%40puppetlabs.com. For more options, visit https://groups.google.com/d/optout.
Re: [Puppet Users] DRY duplicated manifest code
On 23/03/16 21:56, Matt Zagrabelny wrote: On Wed, Mar 23, 2016 at 3:04 PM, Hunter Haugen wrote: Given the resource you want to apply this pattern to, it can be turned into a one-liner with a collector: file { '/tmp/something': ensure => file, } File['/tmp/something'] ~> Service <| title == 'apache2' |> Can you combine the two steps? file { '/tmp/something': ensure => file, } ~> Service <| title == 'apache2' |> or is that frowned upon, or just not possible? That should work. - henrik -- Visit my Blog "Puppet on the Edge" http://puppet-on-the-edge.blogspot.se/ -- 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+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/56F30705.8090701%40puppetlabs.com. For more options, visit https://groups.google.com/d/optout.
Re: [Puppet Users] DRY duplicated manifest code
On Wed, Mar 23, 2016 at 3:04 PM, Hunter Haugen wrote: > Given the resource you want to apply this pattern to, it can be turned into > a one-liner with a collector: > > file { '/tmp/something': > ensure => file, > } > File['/tmp/something'] ~> Service <| title == 'apache2' |> Can you combine the two steps? file { '/tmp/something': ensure => file, } ~> Service <| title == 'apache2' |> or is that frowned upon, or just not possible? -m -- 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+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/CAOLfK3WNnW8p09_Da88VcC5oPXMaKt4rV1R4QL3Verva8ap%2BNw%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.
Re: [Puppet Users] DRY duplicated manifest code
On Wed, Mar 23, 2016 at 3:04 PM, Hunter Haugen wrote: > Given the resource you want to apply this pattern to, it can be turned into > a one-liner with a collector: > > file { '/tmp/something': > ensure => file, > } > File['/tmp/something'] ~> Service <| title == 'apache2' |> > > This means that if there is a service with a title of apache2 EVER added to > the catalog, it'll be refreshed on file changes. If the service doesn't > exist, then the dependency does nothing. > > Now, this isn't exactly what you asked since you wanted the variable > $services_to_notify and didn't say what you're going to do with it, but I > assume this is what you want? Because collectors are not parse-order > specific, you can't do variable assignments like $services_to_notify = > Service <| title == 'apache2' |> (because variables are evaluated in parse > order and collectors are not). > > If you really want to make a function that searches the catalog and returns > references, it can be done with something like > `scope.catalog.resource('Service[apache2]')` inside the function I believe, > though that may not be the exact call. Thanks for the reply, Hunter. I'll dig in and report back if I've got issues. Cheers! -m -- 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+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/CAOLfK3XuVMK_xSWvCQ33qmR0Bywyo5xPaa6fJDannH%2BL5ymBZA%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.
Re: [Puppet Users] DRY duplicated manifest code
Given the resource you want to apply this pattern to, it can be turned into a one-liner with a collector: file { '/tmp/something': ensure => file, } File['/tmp/something'] ~> Service <| title == 'apache2' |> This means that if there is a service with a title of apache2 EVER added to the catalog, it'll be refreshed on file changes. If the service doesn't exist, then the dependency does nothing. Now, this isn't exactly what you asked since you wanted the variable $services_to_notify and didn't say what you're going to do with it, but I assume this is what you want? Because collectors are not parse-order specific, you can't do variable assignments like $services_to_notify = Service <| title == 'apache2' |> (because variables are evaluated in parse order and collectors are not). If you really want to make a function that searches the catalog and returns references, it can be done with something like `scope.catalog.resource('Service[apache2]')` inside the function I believe, though that may not be the exact call. -Hunter On Wed, Mar 23, 2016 at 12:40 PM, Matt Zagrabelny wrote: > Greetings Puppet Users, > > I have a chuck of code I'd like to centralize - you know DRY. > > I've looked into a custom function, but I'm uncertain how to get at > the the puppet resources inside of ruby. > > Here is the verbatim copy of the chuck in a puppet manifest: > > if defined(Service['apache2']) { > $services_to_notify = [ > Service['apache2'], > ] > } > else { > $services_to_notify = [] > } > > and here is some hand-wavy pseudocode: > > function return_service_array_if_defined($service) { > if defined(Service[$service]) { > return [ > Service[$service], > ] > } > else { > return [] > } > } > > Any suggestions or ideas for implementation? > > Thanks! > > -m > > -- > 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+unsubscr...@googlegroups.com. > To view this discussion on the web visit > https://groups.google.com/d/msgid/puppet-users/CAOLfK3V6i82smoDO2kwOYJTiurqdD3O_bt%2BaR4RYUGMsqCPgSw%40mail.gmail.com > . > For more options, visit https://groups.google.com/d/optout. > -- 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+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/CAJaQvGDdEGF8mdSt2b8pxdX49YaY3vg9pnSgnHQJ4d%3D3dQXDxQ%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.
[Puppet Users] DRY duplicated manifest code
Greetings Puppet Users, I have a chuck of code I'd like to centralize - you know DRY. I've looked into a custom function, but I'm uncertain how to get at the the puppet resources inside of ruby. Here is the verbatim copy of the chuck in a puppet manifest: if defined(Service['apache2']) { $services_to_notify = [ Service['apache2'], ] } else { $services_to_notify = [] } and here is some hand-wavy pseudocode: function return_service_array_if_defined($service) { if defined(Service[$service]) { return [ Service[$service], ] } else { return [] } } Any suggestions or ideas for implementation? Thanks! -m -- 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+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/CAOLfK3V6i82smoDO2kwOYJTiurqdD3O_bt%2BaR4RYUGMsqCPgSw%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.