Quickstart

Once you have pypuppetdb installed you can configure it to connect to PuppetDB and take it from there.

Connecting

The first thing you need to do is to connect with PuppetDB:

>>> from pypuppetdb import connect
>>> db = connect()

Nodes

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

Node scope

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

>>> 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

>>> 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.

Project Versions

Table Of Contents

Previous topic

Welcome to pypuppetdb’s documentation!

Next topic

Developer Interface

This Page