Debian Codegraph @

docs/basics.rst

raw
1 .. _basics:
2
3 Basics
4 ======
5
6 The most basic use cases is to get some single entities or multiple
7 entities from the PuppetDB without advanced queries.
8
9 Nodes
10 -----
11
12 The following will return a generator object yielding Node objects for every
13 returned node from PuppetDB.
14
15 .. code-block:: python
16
17 >>> nodes = db.nodes()
18 >>> for node in nodes:
19 >>> print(node)
20 host1
21 host2
22 ...
23
24 To query a single node the singular `node()` method can be used:
25
26 .. code-block:: python
27
28 >>> node = db.node('hostname')
29 >>> print(node)
30 hostname
31
32 Node scope
33 ~~~~~~~~~~
34
35 The Node objects are a bit more special in that they can query for facts and
36 resources themselves. Using those methods from a node object will automatically
37 add a query to the request scoping the request to the node.
38
39 .. code-block:: python
40
41 >>> node = db.node('hostname')
42 >>> print(node.fact('osfamily').value)
43 RedHat
44
45 Facts
46 -----
47
48 .. code-block:: python
49
50 >>> facts = db.facts('osfamily')
51 >>> for fact in facts:
52 >>> print(f"{fact.node} - {fact.value}")
53 host1 - RedHat
54 host2 - Debian
55
56 That queries PuppetDB for the 'osfamily' fact and will yield Fact objects,
57 one per node this fact is known for.
58
59 Resources
60 ---------
61
62 .. code-block:: python
63
64 >>> resources = db.resources('file')
65
66 Will return a generator object containing all file resources you're managing
67 across your infrastructure. This is probably a bad idea if you have a big
68 number of nodes as the response will be huge.
69
70 Catalogs
71 --------
72
73 .. code-block:: python
74
75 >>> catalog = db.catalog('hostname')
76 >>> for res in catalog.get_resources():
77 >>> print(res)
78
79 Will return a Catalog object with the latest Catalog of the defined
80 host. This catalog contains the defined Resources and Edges.
81
82 .. code-block:: python
83
84 >>> catalog = db.catalog('hostname')
85 >>> resource = catalog.get_resource('Service', 'ntp')
86 >>> for rel in resource.relationships:
87 >>> print(rel)
88 Class[Ntp] - contains - Service[ntp]
89 File[/etc/ntp.conf] - notifies - Service[ntp]
90 File[/etc/ntp.conf] - required-by - Service[ntp]
91
92
93 Will return all Relationships of a given Resource defined by type and
94 title. This will list all linked other Resources and the type of
95 relationship.