46 cards
Posted about 3 years ago. Visible to the public. Repeats.

Useful puppetdb queries

The queries in this example are all executed executet via curl local on the puppetdb server. They're piped to jq, so you need jq installed to make use of these queries. Your puppetdb needs to support the query API v4.

Query API v4

get all hostnames of nodes including a specific class

curl -Gs http://localhost:8080/pdb/query/v4/resources/Class/$CLASSNAME | jq '.[].certname' # example curl -Gs http://localhost:8080/pdb/query/v4/resources/Class/Nginx | jq '.[].certname'

get hostnames of nodes with a specific fact value

curl -Gs http://localhost:8080/pdb/query/v4/facts/$FACTNAME/$FACTVALUE | jq '.[].certname' # example curl -Gs http://localhost:8080/pdb/query/v4/facts/lsbdistcodename/precise | jq '.[].certname'

get the value from a fact from all nodes having a specific value from another fact

# replace "select-facts" in puppetdb versions > 2.3 with "select_facts" curl -Gs http://localhost:8080/pdb/query/v4/facts/$FACT_TO_GET --data-urlencode 'query=["in", "certname", ["extract", "certname", ["select-facts", ["and", ["=", "name", "$FACT_TO_CHECK"], ["=", "value", "$FACTVALUE"]]]]]]' | jq '.[].value' # example: receive EC2 public hostnames from all nodes that have lsbdistcodename == xenial curl -Gs http://localhost:8080/pdb/query/v4/facts/ec2_public_hostname --data-urlencode 'query=["in", "certname", ["extract", "certname", ["select-facts", ["and", ["=", "name", "lsbdistcodename"], ["=", "value", "xenial"]]]]]]' | jq '.[].value

Get Resources with a specific parameter

curl -Gs http://localhost:8080/pdb/query/v4/resources/$RESOURCE --data-urlencode 'query=["=", ["parameter", "$PARAMETERNAME"], "$PARAMETERVALUE"]' # example: get title of all file resources with the path etc/collectd/conf.d/logfile.conf curl -Gs http://localhost:8080/pdb/query/v4/resources/File --data-urlencode 'query=["=", ["parameter", "path"], "/etc/collectd/conf.d/logfile.conf"]' | jq '.[].title'

PQL Queries

Make sure you have a running ssh tunnel to the puppetdb, if not you can run connect_puppetdb.

pdbquery is part of our ops-tools-Repo.

Get all nodes with a specific fact and class

# you may leave out the SUBFACT pdbquery 'query=resources { type = "Class" and title = "$CLASSNAME" and nodes { inventory { facts.$FACT.$SUBFACT = "$VALUE"}} }' | jq '.[].certname' # example all nodes with class postgresql and 4 cpu cores pdbquery 'query=resources { type = "Class" and title = "Postgresql::Server" and nodes { inventory { facts.processors.count = 4}} }' | jq '.[].certname'

Owner of this card:

Claus-Theodor Riegg
Last edit:
11 months ago
by Florian Heinle
ops, puppet
This website uses cookies to improve usability and analyze traffic.
Accept or learn more