using generated query string in a collector

26 views
Skip to first unread message

Johan De Wit

unread,
Apr 4, 2016, 8:24:50 AM4/4/16
to puppet...@googlegroups.com
Hi, 

Seems it is not possible, well, I cannot make it work.

Depending on some class parameter, some exported resources should be collected or not.

eg.


class X (
$collect_tags = ['cond1', 'cond2']
) {

$my_cond = my_magic_function($collect_tags) # returns the string "tag == 'cond1' or tag=='cond2'"

My_exp_res <<| $my_cond |>>
}

But the only result I get : Error 400 on SERVER: Syntax error at '|>>'; expected '}'

Whatever i tried, nothing gave me the desired result. Using single quotes, double quotes, escaping quotes ....

And <<| tag == $collect_tags |>> ( <<| tag == ['cond1','cond2'] |>> does not give me the desired result, only one exported resource is collected. But I try to ovoid this, because the behavior of this i unpredictable, as documented in the docs.

I already I'm a big fan of iteration, but this needs to work on puppet 3.[7|8].x

For now I will change my parameter to a single string and us an if clause like ...

if ( $collect_tag =~ /all$/ ) {
My_exp_res <<| |>>
} else {
My_exp_res <<| tag == $collect_tag >>
}

I will loose the ability to collect on multiple nodes this way, but we can live with this for the moment.

Peeking to puppetdbquery as an alternative ....

Grts

Johan


--
Johan De Wit

Open Source Consultant -- Open-Future

Red Hat Certified Engineer (805008667232363)
Puppet Certified Professional 2013/2014/2015 (PCP0000006)
Puppet Certified Instructor
blog : http://johan.koewacht.net/   gsm: +32 474 42 40 73


Felix Frank

unread,
Apr 11, 2016, 8:21:06 PM4/11/16
to puppet...@googlegroups.com
On 04/04/2016 02:24 PM, Johan De Wit wrote:
> I already I'm a big fan of iteration, but this needs to work on puppet
> 3.[7|8].x

Well, there is always "old style pseudo-iteration".

define collect_my_stuff() {
My_exp_res<<| tag == $name |>>
}

class X($collect_tags) {
collect_my_stuff { $collect_tags: }
}

Untested, as always ;-)

HTH,
Felix

jcbollinger

unread,
Apr 12, 2016, 9:05:15 AM4/12/16
to Puppet Users


On Monday, April 4, 2016 at 7:24:50 AM UTC-5, Johan De Wit wrote:
Hi, 

Seems it is not possible, well, I cannot make it work.

Depending on some class parameter, some exported resources should be collected or not.

eg.


class X (
$collect_tags = ['cond1', 'cond2']
) {

$my_cond = my_magic_function($collect_tags) # returns the string "tag == 'cond1' or tag=='cond2'"

My_exp_res <<| $my_cond |>>
}

But the only result I get : Error 400 on SERVER: Syntax error at '|>>'; expected '}'


I can't say I'm a bit surprised.  Shell script is the only language I can think of where something like that can work.  Puppet parses the manifest, *then* evaluates the result; replacing variable references with the corresponding values is an aspect of evaluation.  To put it another way, a Puppet variable reference can represent only a value, not DSL code.

 

Whatever i tried, nothing gave me the desired result. Using single quotes, double quotes, escaping quotes ....

And <<| tag == $collect_tags |>> ( <<| tag == ['cond1','cond2'] |>> does not give me the desired result, only one exported resource is collected. But I try to ovoid this, because the behavior of this i unpredictable, as documented in the docs.


In fact, according to the docs, the behavior is undefined in the Puppet 3.7 and 3.8 series (and in Puppet 4, which you did not mention).  I generally take that as an even more forboding characteristic than "unpredictable".  Calling the behavior "undefined" should be interpreted as meaning "don't do that".

 

I already I'm a big fan of iteration, but this needs to work on puppet 3.[7|8].x

For now I will change my parameter to a single string and us an if clause like ...

if ( $collect_tag =~ /all$/ ) {
My_exp_res <<| |>>
} else {
My_exp_res <<| tag == $collect_tag >>
}



Is enabling the future parser a viable alternative for you?  That would allow you to use the new-style iteration functions even in v3.[78].

 
I will loose the ability to collect on multiple nodes this way, but we can live with this for the moment.

Peeking to puppetdbquery as an alternative ....


I'm not quite seeing how puppetdbquery would help, but Felix's suggestion to build a solution in the old-school way, based on array-titled resource declarations, should work in any version of Puppet from at least 0.24 through 4.4.


John

Henrik Lindberg

unread,
Apr 12, 2016, 10:40:06 AM4/12/16
to puppet...@googlegroups.com
On 12/04/16 15:05, jcbollinger wrote:
> Peeking to puppetdbquery as an alternative ....
>
>
>
> I'm not quite seeing how puppetdbquery would help, but Felix's
> suggestion to build a solution in the old-school way, based on
> array-titled resource declarations, should work in any version of Puppet
> from at least 0.24 through 4.4.

With puppetdb query you should be able to get the resources as data.
With puppet 4 there is syntax to create resources from hashes, in
earlier versions the create_resources function would have to be used.

- henrik

--

Visit my Blog "Puppet on the Edge"
http://puppet-on-the-edge.blogspot.se/
Reply all
Reply to author
Forward
0 new messages