Problem querying PuppetDB from within a Puppet class

13 views
Skip to first unread message

Dirk Heinrichs

unread,
Feb 28, 2019, 7:37:35 AM2/28/19
to puppet...@googlegroups.com
Hi,

following the example in the PuppetDB documentation, I tried this code:

$debian_nodes_query = 'nodes[certname]{facts{name = "operatingsystem" and value = "Debian"}}'
$debian_nodes = puppetdb_query($debian_nodes_query).each |$value| { $value["certname"] }
Notify {"Debian nodes":
    message => "Your debian nodes are ${join($debian_nodes, ', ')}",
}
which prints:

Notice: Your debian nodes are {"certname"=>"host1.example.com"}, {"certname"=>"host2.example.com"}

despite the '$value["certname"]'. What would I need to do to print the names only? Like:

Notice: Your debian nodes are host1.example.com, host2.example.com

which is what I would expect the code above to do in the first place.

Thanks...

Dirk
-- 
Dirk Heinrichs
Senior Systems Engineer, Delivery Pipeline
OpenText ™ Discovery | Recommind
Recommind GmbH, Von-Liebig-Straße 1, 53359 Rheinbach
Vertretungsberechtigte Geschäftsführer John Marshall Doolittle, Gordon Davies, Christian Waida, Registergericht Amtsgericht Bonn, Registernummer HRB 10646
This e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error) please notify the sender immediately and destroy this e-mail. Any unauthorized copying, disclosure or distribution of the material in this e-mail is strictly forbidden
Diese E-Mail enthält vertrauliche und/oder rechtlich geschützte Informationen. Wenn Sie nicht der richtige Adressat sind oder diese E-Mail irrtümlich erhalten haben, informieren Sie bitte sofort den Absender und vernichten Sie diese Mail. Das unerlaubte Kopieren sowie die unbefugte Weitergabe dieser Mail sind nicht gestattet.

Henrik Lindberg

unread,
Feb 28, 2019, 7:54:07 AM2/28/19
to puppet...@googlegroups.com
On 2019-02-28 13:37, Dirk Heinrichs wrote:
> Hi,
>
> following the example in the PuppetDB documentation, I tried this code:
>
> $debian_nodes_query = 'nodes[certname]{facts{name = "operatingsystem"
> and value = "Debian"}}'
> $debian_nodes = puppetdb_query($debian_nodes_query).each |$value| {

The line above is wrong - you are using each() and it returns its input.
Did you mean to use map() to get an array of the names? That is, like this:

$debian_nodes = puppetdb_query($debian_nodes_query).map |$value| {
$value["certname"]
}

- henrik

> $value["certname"] }
> Notify {"Debian nodes":
>     message => "Your debian nodes are ${join($debian_nodes, ', ')}",
> }
> which prints:
>
> Notice: Your debian nodes are {"certname"=>"host1.example.com"},
> {"certname"=>"host2.example.com"}
>
> despite the '$value["certname"]'. What would I need to do to print the
> names only? Like:
>
> Notice: Your debian nodes are host1.example.com, host2.example.com
>
> which is what I would expect the code above to do in the first place.
>
> Thanks...
>
> Dirk
>
> --


--

Visit my Blog "Puppet on the Edge"
http://puppet-on-the-edge.blogspot.se/

Dirk Heinrichs

unread,
Feb 28, 2019, 8:47:07 AM2/28/19
to puppet...@googlegroups.com
Am Donnerstag, den 28.02.2019, 13:53 +0100 schrieb Henrik Lindberg:

On 2019-02-28 13:37, Dirk Heinrichs wrote:
following the example in the PuppetDB documentation, I tried this code:

$debian_nodes_query = 'nodes[certname]{facts{name = "operatingsystem" 
and value = "Debian"}}'
$debian_nodes = puppetdb_query($debian_nodes_query).each |$value| { 

The line above is wrong - you are using each() and it returns its input.

As said, this comes straight from the documentation.

Did you mean to use map() to get an array of the names? That is, like this:

   $debian_nodes = puppetdb_query($debian_nodes_query).map |$value| {
     $value["certname"]
   }

That just prints:

Notice: Your debian nodes are

Bye...

Henrik Lindberg

unread,
Feb 28, 2019, 10:12:35 AM2/28/19
to puppet...@googlegroups.com
On 2019-02-28 14:46, Dirk Heinrichs wrote:
> Am Donnerstag, den 28.02.2019, 13:53 +0100 schrieb Henrik Lindberg:
>
>> On 2019-02-28 13:37, Dirk Heinrichs wrote:
>>> following the example in the PuppetDB documentation, I tried this code:
>>>
>>> $debian_nodes_query = 'nodes[certname]{facts{name = "operatingsystem"
>>> and value = "Debian"}}'
>>> $debian_nodes = puppetdb_query($debian_nodes_query).each |$value| {
>>
>> The line above is wrong - you are using each() and it returns its input.
>
> As said, this comes straight from the documentation.
>
>> Did you mean to use map() to get an array of the names? That is, like
>> this:
>>
>>    $debian_nodes = puppetdb_query($debian_nodes_query).map |$value| {
>>      $value["certname"]
>>    }
>
> That just prints:
>
> Notice: Your debian nodes are
>
> Bye...
>

Where in documentation did you get that example? It is wrong so needs to
be corrected. You definitively need to use `map` to map the result to
what you want.

Try output of all first so you see if you got any, then output the
mapped result.

- henrik

> Dirk
>
> --
>
> *Dirk Heinrichs*
> Senior Systems Engineer, Delivery Pipeline
> OpenText ™ Discovery | Recommind
> *Phone*: +49 2226 15966 18
> *Email*: dhei...@opentext.com <mailto:dhei...@opentext.com>
> *Website*: www.recommind.de
> Recommind GmbH, Von-Liebig-Straße 1, 53359 Rheinbach
> Vertretungsberechtigte Geschäftsführer John Marshall Doolittle, Gordon
> Davies, Christian Waida, Registergericht Amtsgericht Bonn,
> Registernummer HRB 10646
> This e-mail may contain confidential and/or privileged information. If
> you are not the intended recipient (or have received this e-mail in
> error) please notify the sender immediately and destroy this e-mail. Any
> unauthorized copying, disclosure or distribution of the material in this
> e-mail is strictly forbidden
> Diese E-Mail enthält vertrauliche und/oder rechtlich geschützte
> Informationen. Wenn Sie nicht der richtige Adressat sind oder diese
> E-Mail irrtümlich erhalten haben, informieren Sie bitte sofort den
> Absender und vernichten Sie diese Mail. Das unerlaubte Kopieren sowie
> die unbefugte Weitergabe dieser Mail sind nicht gestattet.
>
> --
> 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...@googlegroups.com
> <mailto:puppet-users...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/puppet-users/948f3fb601109b2876b32125a84167bcaa4e166e.camel%40opentext.com
> <https://groups.google.com/d/msgid/puppet-users/948f3fb601109b2876b32125a84167bcaa4e166e.camel%40opentext.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.

Henrik Lindberg

unread,
Feb 28, 2019, 10:45:26 AM2/28/19
to puppet...@googlegroups.com
On 2019-02-28 16:12, Henrik Lindberg wrote:
> Where in documentation did you get that example? It is wrong so needs to
> be corrected. You definitively need to use `map` to map the result to
> what you want.

Already fixed in later versions of the documentation: ticket is here:
https://tickets.puppetlabs.com/browse/DOCUMENT-816

- henrik

Dirk Heinrichs

unread,
Mar 6, 2019, 10:10:14 AM3/6/19
to puppet...@googlegroups.com
Am Donnerstag, den 28.02.2019, 13:46 +0000 schrieb Dirk Heinrichs:

Am Donnerstag, den 28.02.2019, 13:53 +0100 schrieb Henrik Lindberg:

Did you mean to use map() to get an array of the names? That is, like this:

   $debian_nodes = puppetdb_query($debian_nodes_query).map |$value| {
     $value["certname"]
   }

That just prints:

Notice: Your debian nodes are

Got it working. When trying with .map, I copied the code verbatim again, but my "operating" system is Ubuntu, not Debian.
Reply all
Reply to author
Forward
0 new messages