I have an array like this: $items = [ "a", "-b" ] That should generate something like this (let''s say for the Cron resource: Cron <| (tag == "a" or title == "a") and (tag != "b" or title != "b") |>The reason is because we are grouping sets of virtual resources with tags to be realized (as a whole) but sometimes we don''t want an item of that group to get realized. Like: @cron { "a": tag => "a", } @cron { "b": tag => "a", } @cron { "c": tag => "a", } All this is generated dynamically... I can write a function to generate the string like: $to_realize = (tag == "a" or title == "a") and (tag != "b" or title !"b") but if I do Cron <| $to_realize |> It fails... brutally. Any ideas? Thank you. -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To post to this group, send email to puppet-users@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.
The short answer is: it sucks to be you. The longer answer is that variables are really rvalues: they are something that you can read and test, but they are not function calls or otherwise subject to evaluation. When you write Cron <| tag == "a" |> in your manifest, you are evaluating an == expression using the values on either side. When you write Cron <| $complicated_string |> there is no test in there! It''s just a string. The content of the string can be arbitrarily complex, but it will *never* be evaluated as anything other than a string. Puppet has a declarative language, not imperative. And like all declarative languages, it suffers from the "there are no shortcuts" issue. You have to declare everything explicitly. There is no supported way to really to do anything dynamic such as you are trying to do. All that said, evil tricks lurk in the land of dragons. Don''t bother asking for help if you go down this particular path, because the Puppetlabs people will only tell you that "you''re doing it wrong" and no one else will be willing to admit that you''ve pervert Puppet in this manner. All that said, think about this: you can write functions that will, in turn, be available from within Puppet manifests. Those functions, written in Ruby, can do anything that Ruby can do. Puppet is written in Ruby, and all of the internal library bits are available to your function, if only you bend enough programming and convention rules, as well as discovering all of the (possibly intentionally undocumented) internals necessary to do what you need. As a starting point, read (and understand) the code in puppet-2.6.6/lib/puppet/parser/collector.rb. You essentially want to write your own function that behaves very similarly to the code in there. On Thu, Mar 10, 2011 at 3:56 PM, Roberto Bouza <bouzafr@gmail.com> wrote:> I have an array like this: > > $items = [ "a", "-b" ] > > That should generate something like this (let''s say for the Cron > resource: > > Cron <| (tag == "a" or title == "a") and (tag != "b" or title != "b") | > > > > The reason is because we are grouping sets of virtual resources with > tags to be realized (as a whole) but sometimes we don''t want an item > of that group to get realized. > > Like: > > @cron { "a": > tag => "a", > } > > @cron { "b": > tag => "a", > } > > @cron { "c": > tag => "a", > } > > All this is generated dynamically... I can write a function to > generate the string like: > > $to_realize = (tag == "a" or title == "a") and (tag != "b" or title !> "b") > > but if I do > > Cron <| $to_realize |> > > It fails... brutally. > > Any ideas? > > Thank you. > > -- > You received this message because you are subscribed to the Google Groups > "Puppet Users" group. > To post to this group, send email to puppet-users@googlegroups.com. > To unsubscribe from this group, send email to > puppet-users+unsubscribe@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/puppet-users?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To post to this group, send email to puppet-users@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.
On Mar 10, 6:56 pm, Roberto Bouza <bouz...@gmail.com> wrote:> I have an array like this: > > $items = [ "a", "-b" ] > > That should generate something like this (let''s say for the Cron > resource: > > Cron <| (tag == "a" or title == "a") and (tag != "b" or title != "b") | > > > > The reason is because we are grouping sets of virtual resources with > tags to be realized (as a whole) but sometimes we don''t want an item > of that group to get realized. > > Like: > > @cron { "a": > tag => "a", > > } > > @cron { "b": > tag => "a", > > } > > @cron { "c": > tag => "a", > > } > > All this is generated dynamically... I can write a function to > generate the string like: > > $to_realize = (tag == "a" or title == "a") and (tag != "b" or title !> "b") > > but if I do > > Cron <| $to_realize |> > > It fails... brutally. > > Any ideas?If you''re on 2.6, then this may be a good use case for Puppet''s Ruby DSL. Depending on how general the facility needs to be, you may be able to implement it in ordinary Puppet DSL with via of defines. By "how general", I''m mostly thinking about whether you can live with separate definitions behind the scenes for excluding zero items, one item, etc.. I think it ought to be possible to hide all such mess behind a common front end definition, but I don''t think this approach can support an arbitrary number of exclusions. The simplest approach, however, may be to tweak the tags on your virtual resources to avoid their realization, instead of using filter conditions for the same job. That might not work well for exported resources, but it should be fine for simple virtual ones. For example, @cron { "b": tag => $realize_me ? { "no" => undef, default => "b" } } Or you could even do this: if $suppress_b != "yes" { @cron { "b": tag => "b" } } John -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To post to this group, send email to puppet-users@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.
Roberto Bouza
2011-Mar-14 19:42 UTC
[Puppet Users] Re: How can I dynamically realize this?
Ha!!! That is a nice approach... Let me try that one and I''ll get back to you on this. Thank you. On Mar 14, 5:50 am, jcbollinger <John.Bollin...@stJude.org> wrote:> On Mar 10, 6:56 pm, Roberto Bouza <bouz...@gmail.com> wrote: > > > > > > > I have an array like this: > > > $items = [ "a", "-b" ] > > > That should generate something like this (let''s say for the Cron > > resource: > > > Cron <| (tag == "a" or title == "a") and (tag != "b" or title != "b") | > > > The reason is because we are grouping sets of virtual resources with > > tags to be realized (as a whole) but sometimes we don''t want an item > > of that group to get realized. > > > Like: > > > @cron { "a": > > tag => "a", > > > } > > > @cron { "b": > > tag => "a", > > > } > > > @cron { "c": > > tag => "a", > > > } > > > All this is generated dynamically... I can write a function to > > generate the string like: > > > $to_realize = (tag == "a" or title == "a") and (tag != "b" or title !> > "b") > > > but if I do > > > Cron <| $to_realize |> > > > It fails... brutally. > > > Any ideas? > > If you''re on 2.6, then this may be a good use case for Puppet''s Ruby > DSL. > > Depending on how general the facility needs to be, you may be able to > implement it in ordinary Puppet DSL with via of defines. By "how > general", I''m mostly thinking about whether you can live with separate > definitions behind the scenes for excluding zero items, one item, > etc.. I think it ought to be possible to hide all such mess behind a > common front end definition, but I don''t think this approach can > support an arbitrary number of exclusions. > > The simplest approach, however, may be to tweak the tags on your > virtual resources to avoid their realization, instead of using filter > conditions for the same job. That might not work well for exported > resources, but it should be fine for simple virtual ones. For > example, > > @cron { "b": > tag => $realize_me ? { > "no" => undef, > default => "b" > } > > } > > Or you could even do this: > > if $suppress_b != "yes" { > @cron { "b": > tag => "b" > } > > } > > John-- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To post to this group, send email to puppet-users@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.
Nigel Kersten
2011-Mar-15 00:12 UTC
Re: [Puppet Users] How can I dynamically realize this?
On Thu, Mar 10, 2011 at 9:27 PM, Brian Gallew <geek@gallew.org> wrote:> All that said, evil tricks lurk in the land of dragons. Don''t bother asking > for help if you go down this particular path, because the Puppetlabs people > will only tell you that "you''re doing it wrong" and no one else will be > willing to admit that you''ve pervert Puppet in this manner.I consider that a pretty unfair characterization of how "Puppet Labs people" respond to questions. The Ruby DSL is a good place to look to implement this, another option might be to make use of the realize function instead, and build up arrays of resource references, or even copy/modify the realize function to make the expression of this in the Puppet DSL simpler and more obvious. -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To post to this group, send email to puppet-users@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.