blob: 97d78afcc977524b4c95eb781e4f7dc7ec593258 [file] [log] [blame]
Petr Michalecad441172017-09-18 17:18:10 +02001Escaping of References and Inventory Queries
2--------------------------------------------
3
4Reference and inventory queries can be escaped to produce literal strings, for example:
5
6.. code-block:: yaml
7
8 parameters:
9 colour: Blue
10 unescaped: The colour is ${colour}
11 escaped: The colour is \${colour}
12 double_escaped: The colour is \\${colour}
13
14
15This would produce:
16
17.. code-block:: yaml
18
19 parameters:
20 colour: Blue
21 unescaped: The colour is Blue
22 escaped: The colour is ${colour}
23 double_escaped: The colour is \Blue
24
25
26
27Ignore class not found
28----------------------
29
30At some cases (bootstrapping, development) it can be convenient to ignore some missing classes.
31To control the feature there are two options available:
32
33.. code-block:: yaml
34
35 ignore_class_notfound: False
Petr Michalec19324742017-09-18 17:32:24 +020036 ignore_class_regexp: ['.*']
Petr Michalecad441172017-09-18 17:18:10 +020037
38If you set regexp pattern to ``service.*`` all missing classes starting 'service.' will be logged with warning, but will not
39fail to return rendered reclass. Assuming all parameter interpolation passes.
40
41
42
43Merging Referenced Lists and Dictionaries
44-----------------------------------------
45
46Referenced lists or dicts can now be merged:
47
48.. code-block:: yaml
49
50 # nodes/test.yml
51 classes:
52 - test1
53 - test2
54 parameters:
55 one:
56 a: 1
57 b: 2
58 two:
59 c: 3
60 d: 4
61 three:
62 e: 5
63
64 # classes/test1.yml
65 parameters:
66 three: ${one}
67
68 # classes/test2.yml
69 parameters:
70 three: ${two}
71
72``running reclass.py --nodeinfo node1`` then gives:
73
74.. code-block:: yaml
75
76 parameters:
77 one:
78 a: 1
79 b: 2
80 three:
81 a: 1
82 b: 2
83 c: 3
84 d: 4
85 e: 5
86 two:
87 c: 3
88 d: 4
89
90This first sets the parameter three to the value of parameter one (class test1) then merges parameter two into
91parameter three (class test2) and finally merges the parameter three definition given in the node definition into
92the final value.
93
94
Petr Michalec0a3a3682018-03-28 15:30:15 +020095Allow override list and dicts by empty entity,None instead of merge
96-------------------------------------------------------------------
97
98With settings:
99
100.. code-block:: yaml
101
102 allow_none_override: True # default True
103
104 # note dict,list over None is allowed and not configurable
105
106Referenced lists or dicts can now be overriden by None or empty type of dict, list:
107
108.. code-block:: yaml
109
110 # nodes/test.yml
111 parameters:
112 one:
113 a: 1
114 b: 2
115 two: {}
116 three: None
117
118 # classes/test1.yml
119 parameters:
120 one: ${two}
121
122 # classes/test2.yml
123 parameters:
124 three: ${one}
125
Petr Michalecad441172017-09-18 17:18:10 +0200126
127Nested References
128-----------------
129
130References can now be nested, for example:
131
132.. code-block:: yaml
133
134 # nodes/node1.yml
135 parameters:
136 alpha:
137 one: ${beta:${alpha:two}}
138 two: a
139 beta:
140 a: 99
141
142``reclass.py --nodeinfo node1`` then gives:
143
144.. code-block:: yaml
145
146 parameters:
147 alpha:
148 one: 99
149 two: a
150 beta:
151 a: 99
152
153The ``${beta:${alpha:two}}`` construct first resolves the ``${alpha:two}`` reference to the value 'a', then resolves
154the reference ``${beta:a}`` to the value 99.
155
156
Andrew Pickforde0eb7b62018-03-16 08:45:42 +0100157Ignore overwritten missing references
158-------------------------
159
160Given the following classes:
161
162.. code-block:: yaml
163 # node1.yml
164 classes:
165 - class1
166 - class2
167 - class3
168
169 # class1.yml
170 parameters:
171 a: ${x}
172
173 # class2.yml
174 parameters:
175 a: ${y}
176
177 # class3.yml
178 parameters:
179 y: 1
180
181
182The parameter ``a`` only depends on the parameter ``y`` through the reference set in class2. The fact that the parameter ``x`` referenced
183in class1 is not defined does not affect the final value of the parameter ``a``. For such overwritten missing references by default a warning is
184printed but no error is raised, providing the final value of the parameter being evaluated is a scalar. If the final value is a dictionary or list
185an error will always be raised in the case of a missing reference.
186
187Default value is True to keep backward compatible behavior.
188
189.. code-block:: yaml
190
191 ignore_overwritten_missing_reference: True
192
Petr Michalecad441172017-09-18 17:18:10 +0200193
Andrew Pickfordffd77b42018-03-16 14:37:03 +0100194Print summary of missed references
195----------------------------------
196
197Instead of failing on the first undefinded reference error all missing reference errors are printed at once.
198
199.. code-block:: yaml
200 reclass --nodeinfo mynode
201 -> dontpanic
202 Cannot resolve ${_param:kkk}, at mkkek3:tree:to:fail, in yaml_fs:///test/classes/third.yml
203 Cannot resolve ${_param:kkk}, at mkkek3:tree:another:xxxx, in yaml_fs:///test/classes/third.yml
204 Cannot resolve ${_param:kkk}, at mykey2:tree:to:fail, in yaml_fs:///test/classes/third.yml
205
206.. code-block:: yaml
207
208 group_errors: True
209
210
Petr Michalecad441172017-09-18 17:18:10 +0200211Inventory Queries
212-----------------
213
214Inventory querying works using a new key type - exports to hold values which other node definitions can read using a $[] query, for example with:
215
216.. code-block:: yaml
217
218 # nodes/node1.yml
219 exports:
220 test_zero: 0
221 test_one:
222 name: ${name}
223 value: 6
224 test_two: ${dict}
225
226 parameters:
227 name: node1
228 dict:
229 a: 1
230 b: 2
231 exp_value_test: $[ exports:test_two ]
232 exp_if_test0: $[ if exports:test_zero == 0 ]
233 exp_if_test1: $[ exports:test_one if exports:test_one:value == 7 ]
234 exp_if_test2: $[ exports:test_one if exports:test_one:name == self:name ]
235
236 # nodes/node2.yml
237 exports:
238 test_zero: 0
239 test_one:
240 name: ${name}
241 value: 7
242 test_two: ${dict}
243
244 parameters:
245 name: node2
246 dict:
247 a: 11
248 b: 22
249
250
251``running reclass.py --nodeinfo node1`` gives (listing only the exports and parameters):
252
253.. code-block:: yaml
254
255 exports:
256 test_one:
257 name: node1
258 value: 6
259 test_two:
260 a: 1
261 b: 2
262 parameters:
263 dict:
264 a: 1
265 b: 2
266 exp_if_test0:
267 - node1
268 - node2
269 exp_if_test1:
270 node2:
271 name: node2
272 value: 7
273 exp_if_test2:
274 node1:
275 name: node1
276 value: 6
277 exp_value_test:
278 node1:
279 a: 1
280 b: 2
281 node2:
282 a: 11
283 b: 22
284 name: node1
285
286
287Exports defined for a node can be a simple value or a reference to a parameter in the node definition.
Petr Michalecab9cac32017-09-18 17:35:54 +0200288The ``$[]`` inventory queries are calculated for simple value expressions, ``$[ exports:key ]``, by returning
289a dictionary with an element (``{ node_name: key value }``) for each node which defines 'key' in the exports
290section. For tests with a preceeding value, ``$[ exports:key if exports:test_key == test_value ]``, the
291element (``{ node_name: key value }``) is only added to the returned dictionary if the test_key defined in
Petr Michalecad441172017-09-18 17:18:10 +0200292the node exports section equals the test value. For tests without a preceeding value,
Petr Michalecab9cac32017-09-18 17:35:54 +0200293``$[ if exports:test_key == test_value ]``, a list of nodes which pass the test is returned. For either test
Petr Michalecad441172017-09-18 17:18:10 +0200294form the test value can either be a simple value or a node parameter. And as well as an equality test
Petr Michalecab9cac32017-09-18 17:35:54 +0200295a not equals test (``!=``) can also be used.
Petr Michalecad441172017-09-18 17:18:10 +0200296
297
298**Inventory query options**
299
300By default inventory queries only look at nodes in the same environment as the querying node. This can be
301overriden using the +AllEnvs option:
302
Petr Michalecab9cac32017-09-18 17:35:54 +0200303.. code-block:: yaml
304
Petr Michalecad441172017-09-18 17:18:10 +0200305 $[ +AllEnvs exports:test ]
306
307Any errors in rendering the export parameters for a node will give an error for the inventory query as a whole.
Petr Michalecab9cac32017-09-18 17:35:54 +0200308This can be overriden using the ``+IgnoreErrors`` option:
309
310.. code-block:: yaml
Petr Michalecad441172017-09-18 17:18:10 +0200311
312 $[ +IgnoreErrors exports:test ]
313
Petr Michalecab9cac32017-09-18 17:35:54 +0200314With the ``+IgnoreErrors`` option nodes which generate an error evaluating ``exports:test`` will be ignored.
Petr Michalecad441172017-09-18 17:18:10 +0200315
316Inventory query options can be combined:
317
Petr Michalecab9cac32017-09-18 17:35:54 +0200318.. code-block:: yaml
319
Petr Michalecad441172017-09-18 17:18:10 +0200320 $[ +AllEnvs +IgnoreErrors exports:test ]
321
322**Logical operators and/or**
323
324The logical operators and/or can be used in inventory queries:
325
Petr Michalecab9cac32017-09-18 17:35:54 +0200326.. code-block:: yaml
327
Petr Michalecad441172017-09-18 17:18:10 +0200328 $[ exports:test_value if exports:test_zero == 0 and exports:test_one == self:value ]
329
330The individual elements of the if statement are evaluated and combined with the logical operators starting from the
331left and working to the right.
332
333
334**Inventory query example**
335
336Defining a cluster of machines using an inventory query, for example to open access to a database server to a
337group of nodes. Given exports/parameters for nodes of the form:
338
339.. code-block:: yaml
340
Petr Michalecab9cac32017-09-18 17:35:54 +0200341 # for all nodes requiring access to the database server
342 exports:
343 host:
344 ip_address: aaa.bbb.ccc.ddd
345 cluster: _some_cluster_name_
Petr Michalecad441172017-09-18 17:18:10 +0200346
347.. code-block:: yaml
348
Petr Michalecab9cac32017-09-18 17:35:54 +0200349 # for the database server
350 parameters:
351 cluster_name: production-cluster
352 postgresql:
353 server:
354 clients: $[ exports:host:ip_address if exports:cluster == self:cluster_name ]
Petr Michalecad441172017-09-18 17:18:10 +0200355
Petr Michalecab9cac32017-09-18 17:35:54 +0200356This will generate a dictionary with an entry for node where the ``export:cluster`` key for a node is equal to the
357``parameter:cluster_name`` key of the node on which the inventory query is run on. Each entry in the generated dictionary
358will contain the value of the ``exports:host:ip_address`` key. The output dictionary (depending on node definitions)
Petr Michalecad441172017-09-18 17:18:10 +0200359would look like:
360
361.. code-block:: yaml
362
Petr Michalecab9cac32017-09-18 17:35:54 +0200363 node1:
364 ip_address: aaa.bbb.ccc.ddd
365 node2:
366 ip_address: www.xxx.yyy.zzz
Petr Michalecad441172017-09-18 17:18:10 +0200367
368For nodes where exports:cluster key is not defined or where the key is not equal to self:cluster_name no entry is made
369in the output dictionary.
370
371In practise the exports:cluster key can be set using a parameter reference:
372
373.. code-block:: yaml
374
Petr Michalecab9cac32017-09-18 17:35:54 +0200375 exports:
376 cluster: ${cluster_name}
377 parameters:
378 cluster_name: production-cluster
Petr Michalecad441172017-09-18 17:18:10 +0200379
380The above exports and parameter definitions could be put into a separate class and then included by nodes which require
381access to the database and included by the database server as well.