| 1 |
.. _advanced_queries: |
| 2 |
|
| 3 |
Advanced Queries |
| 4 |
================ |
| 5 |
|
| 6 |
For more advanced use of pypuppetdb you will want to use queries in one of the |
| 7 |
two available languages: |
| 8 |
|
| 9 |
* **PQL** (Puppet Query Language) - newer, arguably more human-friendly. Available |
| 10 |
since PuppetDB v. 4.0. |
| 11 |
|
| 12 |
* **AST** (Abstract Syntax Tree) - older, using the Reverse Polish Notation. |
| 13 |
|
| 14 |
|
| 15 |
PQL query with rich types |
| 16 |
------------------------- |
| 17 |
|
| 18 |
Making a PQL query using ``pql()`` method without using projection for one of the supported types |
| 19 |
(``nodes``, ``edges``, ``facts``, ``resources``, ``catalogs``, ``events``, ``reports``, ``inventory``) |
| 20 |
will return the rich types. |
| 21 |
|
| 22 |
For example the below code returns objects of the type ``Node``: |
| 23 |
|
| 24 |
.. code-block:: python |
| 25 |
|
| 26 |
>>> nodes = db.pql(""" |
| 27 |
>>> nodes { |
| 28 |
>>> facts { |
| 29 |
>>> name = "operatingsystem" |
| 30 |
>>> and value = "Debian" |
| 31 |
>>> } |
| 32 |
>>> } |
| 33 |
>>> """ |
| 34 |
>>> for node in nodes: |
| 35 |
>>> print(node.name) |
| 36 |
foo.example.com |
| 37 |
bar.example.com |
| 38 |
|
| 39 |
|
| 40 |
PQL query returning raw dicts |
| 41 |
----------------------------- |
| 42 |
|
| 43 |
If you do use projections (fields/functions) in your PQL query or you query for a unsupported type, |
| 44 |
you will get JSON like you would querying PuppetDB directly, just converted into a Python dicts |
| 45 |
or list of dicts. |
| 46 |
|
| 47 |
For example if you want to get just a list of certnames of nodes that have the fact "operatingsystem" |
| 48 |
set to "Debian": |
| 49 |
|
| 50 |
.. code-block:: python |
| 51 |
|
| 52 |
>>> nodes = db.pql(""" |
| 53 |
>>> nodes[certname] { |
| 54 |
>>> facts { |
| 55 |
>>> name = "operatingsystem" |
| 56 |
>>> and value = "Debian" |
| 57 |
>>> } |
| 58 |
>>> } |
| 59 |
>>> """ |
| 60 |
>>> for node in nodes: |
| 61 |
>>> print(node) |
| 62 |
{'certname': 'foo.example.com'} |
| 63 |
{'certname': 'bar.example.com'} |
| 64 |
|
| 65 |
Please see `the official PQL documentation <https://puppet.com/docs/puppetdb/7/api/query/v4/pql.html>`_ |
| 66 |
for more examples. |
| 67 |
|
| 68 |
|
| 69 |
AST query |
| 70 |
--------- |
| 71 |
|
| 72 |
Alternatively to use the AST query language, use one of these methods: |
| 73 |
|
| 74 |
``nodes()``, ``edges()``, ``facts()``, ``resources()``, ``catalogs()``, ``events()``, |
| 75 |
``reports()``, ``inventory()``, |
| 76 |
``event_counts()``, ``aggregate_event_counts()``, |
| 77 |
``environments()``, ``factsets()``, ``fact_contents()``, ``fact_paths()`` |
| 78 |
|
| 79 |
...and pass the AST query in the ``query`` parameter. |
| 80 |
|
| 81 |
An example of the same query as above but rewritten in AST: |
| 82 |
|
| 83 |
.. code-block:: python |
| 84 |
|
| 85 |
>>> nodes = db.nodes(query=""" |
| 86 |
>>> ["subquery", "facts", |
| 87 |
>>> ["and", |
| 88 |
>>> ["=", "name", "operatingsystem"], |
| 89 |
>>> ["=", "value", "Debian"] |
| 90 |
>>> ] |
| 91 |
>>> ] |
| 92 |
>>> """ |
| 93 |
>>> for node in nodes: |
| 94 |
>>> print(node.name) |
| 95 |
foo.example.com |
| 96 |
bar.example.com |
| 97 |
|
| 98 |
Please see `the official AST documentation <https://puppet.com/docs/puppetdb/7/api/query/v4/ast.html>`_ |
| 99 |
for more examples . |
| 100 |
|
| 101 |
AST QueryBuilder |
| 102 |
^^^^^^^^^^^^^^^^ |
| 103 |
|
| 104 |
pypuppetdb laso comes shipped with a ``QueryBuilder`` component that, as the name suggests, |
| 105 |
allows users to build PuppetDB AST queries in an Object-Oriented fashion. |
| 106 |
In many cases adding additional clauses to fulfill new requirements is superior to constructing |
| 107 |
long strings. |
| 108 |
|
| 109 |
Above example rewritten using it: |
| 110 |
|
| 111 |
.. code-block:: python |
| 112 |
|
| 113 |
>>> from pypuppetdb.QueryBuilder import * |
| 114 |
>>> op = InOperator('certname') |
| 115 |
>>> query = ExtractOperator() |
| 116 |
>>> query.add_field(str('certname')) |
| 117 |
>>> subquery = SubqueryOperator('facts') |
| 118 |
>>> _add = AndOperator() |
| 119 |
>>> _add.add(EqualsOperator('name', 'operatingsystem')) |
| 120 |
>>> _add.add(EqualsOperator('value', 'Debian')) |
| 121 |
>>> subquery.add_query(_add) |
| 122 |
>>> query.add_query(subquery) |
| 123 |
>>> op.add_query(query) |
| 124 |
>>> print(op) |
| 125 |
["in", "certname", ["extract", ["certname"], ["select_facts", ["and", ["=", "name", "operatingsystem"], ["=", "value", "Debian"]]]]] |
| 126 |
>>> nodes = db.nodes(query=query) |
| 127 |
>>> for node in nodes: |
| 128 |
>>> print(node.name) |
| 129 |
foo.example.com |
| 130 |
bar.example.com |
| 131 |
|
| 132 |
|
| 133 |
The following code will build a query for the Nodes endpoint to find all |
| 134 |
nodes belonging to the production environment. |
| 135 |
|
| 136 |
.. code-block:: python |
| 137 |
|
| 138 |
>>> from pypuppetdb.QueryBuilder import * |
| 139 |
>>> op = AndOperator() |
| 140 |
>>> op.add(EqualsOperator('catalog_environment', 'production')) |
| 141 |
>>> op.add(EqualsOperator('facts_environment', 'production')) |
| 142 |
>>> print(op) |
| 143 |
["and",["=", "catalog_environment", "production"],["=", "facts_environment", "production"]] |
| 144 |
|
| 145 |
|
| 146 |
Subqueries are implemented using corresponding operators (like documented). |
| 147 |
|
| 148 |
* SubqueryOperator |
| 149 |
* InOperator |
| 150 |
* ExtractOperator |
| 151 |
|
| 152 |
.. code-block:: python |
| 153 |
|
| 154 |
>>> from pypuppetdb.QueryBuilder import * |
| 155 |
>>> op = InOperator('certname') |
| 156 |
>>> ex = ExtractOperator() |
| 157 |
>>> ex.add_field(str('certname')) |
| 158 |
>>> sub = SubqueryOperator('events') |
| 159 |
>>> sub.add_query(EqualsOperator('status', 'noop')) |
| 160 |
>>> ex.add_query(sub) |
| 161 |
>>> op.add_query(ex) |
| 162 |
>>> print(op) |
| 163 |
["in","certname",["extract",["certname"],["select_events",["=", "status", "noop"]]]] |
| 164 |
|
| 165 |
Or using in \<array\> querying: |
| 166 |
|
| 167 |
.. code-block:: python |
| 168 |
|
| 169 |
>>> from pypuppetdb.QueryBuilder import * |
| 170 |
>>> op = InOperator('certname') |
| 171 |
>>> op.add_array(["prod1.server.net", "prod2.server.net"]) |
| 172 |
>>> print(op) |
| 173 |
["in","certname",["array", ['prod1.server.net', 'prod2.server.net']]] |
| 174 |
|
| 175 |
|
| 176 |
You can also access different entities from a single query on the root |
| 177 |
endpoint with the FromOperator: |
| 178 |
|
| 179 |
.. code-block:: python |
| 180 |
|
| 181 |
>>> op = InOperator('certname') |
| 182 |
>>> ex = ExtractOperator() |
| 183 |
>>> ex.add_field('certname') |
| 184 |
>>> fr = FromOperator('fact_contents') |
| 185 |
>>> nd = AndOperator() |
| 186 |
>>> nd.add(EqualsOperator("path", ["networking", "eth0", "macaddresses", 0])) |
| 187 |
>>> nd.add(EqualsOperator("value", "aa:bb:cc:dd:ee:00")) |
| 188 |
>>> ex.add_query(nd) |
| 189 |
>>> fr.add_query(ex) |
| 190 |
>>> op.add_query(fr) |
| 191 |
>>> print(op) |
| 192 |
["in","certname",["from","fact_contents",["extract",["certname"],["and",["=", "path", ['networking', 'eth0', 'macaddresses', 0]],["=", "value", "aa:bb:cc:dd:ee:00"]]]]] |