Once you have pypuppetdb installed you can configure it to connect to PuppetDB and take it from there.
The first thing you need to do is to connect with PuppetDB:
>>> from pypuppetdb import connect
>>> db = connect()
The following will return a generator object yielding Node objects for every returned node from PuppetDB.
>>> nodes = db.nodes()
>>> for node in nodes:
>>> print(node)
host1
host2
...
To query a single node the singular node() can be used:
>>> node = db.node('hostname')
>>> print(node)
hostname
The Node objects are a bit more special in that they can query for facts and resources themselves. Using those methods from a node object will automatically add a query to the request scoping the request to the node.
>>> node = db.node('hostname')
>>> print(node.fact('osfamily'))
osfamily/hostname
>>> facts = db.facts('osfamily')
>>> for fact in facts:
>>> print(fact)
osfamily/host1
osfamily/host2
That queries PuppetDB for the ‘osfamily’ fact and will yield Fact objects, one per node this fact is known for.
>>> resources = db.resources('file')
Will return a generator object containing all file resources you’re managing across your infrastructure. This is probably a bad idea if you have a big number of nodes as the response will be huge.