Re: [Puppet Users] How can I dynamically realize this?

Brian Gallew
Fri, 11 Mar 2011 05:27:50 -0000

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.