blob: 8c7c35c072aadf790bda411b76c38e51037f4339 [file] [log] [blame]
Andrew Pickfordbb0a6192017-09-08 16:04:24 +02001Escaping of References and Inventory Queries
2============================================
3
4Reference and inventory queries can be escaped to produce literal strings, for example:
5
6parameters:
7 colour: Blue
8 unescaped: The colour is ${colour}
9 escaped: The colour is \${colour}
10 double_escaped: The colour is \\${colour}
11
12
13This would produce:
14
15parameters:
16 colour: Blue
17 unescaped: The colour is Blue
18 escaped: The colour is ${colour}
19 double_escaped: The colour is \Blue
20
21
22
23Merging Referenced Lists and Dictionaries
24=========================================
25
26Referenced lists or dicts can now be merged:
27
28# nodes/test.yml
29classes:
30 - test1
31 - test2
32parameters:
33 one:
34 a: 1
35 b: 2
36 two:
37 c: 3
38 d: 4
39 three:
40 e: 5
41
42# classes/test1.yml
43parameters:
44 three: ${one}
45
46# classes/test2.yml
47parameters:
48 three: ${two}
49
50running reclass.py --nodeinfo node1 then gives:
51
52parameters:
53 one:
54 a: 1
55 b: 2
56 three:
57 a: 1
58 b: 2
59 c: 3
60 d: 4
61 e: 5
62 two:
63 c: 3
64 d: 4
65
66This first sets the parameter three to the value of parameter one (class test1) then merges parameter two into
67parameter three (class test2) and finally merges the parameter three definition given in the node definition into
68
69
70
71Nested References
72=================
73
74References can now be nested, for example:
75
76# nodes/node1.yml
77parameters:
78 alpha:
79 one: ${beta:${alpha:two}}
80 two: a
81 beta:
82 a: 99
83
84reclass.py --nodeinfo node1 then gives:
85
86parameters:
87 alpha:
88 one: 99
89 two: a
90 beta:
91 a: 99
92
93The ${beta:${alpha:two}} construct first resolves the ${alpha:two} reference to the value 'a', then resolves
94the reference ${beta:a} to the value 99.
95
96
97
98Inventory Queries
99=================
100
101Inventory querying works using a new key type - exports to hold values which other node definitions can read using a $[] query, for example with:
102
103# nodes/node1.yml
104exports:
105 test_zero: 0
106 test_one:
107 name: ${name}
108 value: 6
109 test_two: ${dict}
110
111parameters:
112 name: node1
113 dict:
114 a: 1
115 b: 2
116 exp_value_test: $[ exports:test_two ]
117 exp_if_test0: $[ if exports:test_zero == 0 ]
118 exp_if_test1: $[ exports:test_one if exports:test_one:value == 7 ]
119 exp_if_test2: $[ exports:test_one if exports:test_one:name == self:name ]
120
121# nodes/node2.yml
122exports:
123 test_zero: 0
124 test_one:
125 name: ${name}
126 value: 7
127 test_two: ${dict}
128
129parameters:
130 name: node2
131 dict:
132 a: 11
133 b: 22
134
135
136running reclass.py --nodeinfo node1 gives (listing only the exports and parameters):
137
138exports:
139 test_one:
140 name: node1
141 value: 6
142 test_two:
143 a: 1
144 b: 2
145parameters:
146 dict:
147 a: 1
148 b: 2
149 exp_if_test0:
150 - node1
151 - node2
152 exp_if_test1:
153 node2:
154 name: node2
155 value: 7
156 exp_if_test2:
157 node1:
158 name: node1
159 value: 6
160 exp_value_test:
161 node1:
162 a: 1
163 b: 2
164 node2:
165 a: 11
166 b: 22
167 name: node1
168
169
170Exports defined for a node can be a simple value or a reference to a parameter in the node definition.
171The $[] inventory queries are calculated for simple value expressions, $[ exports:key ], by returning
172a dictionary with an element ({ node_name: key value }) for each node which defines 'key' in the exports
173section. For tests with a preceeding value, $[ exports:key if exports:test_key == test_value ], the
174element ({ node_name: key value }) is only added to the returned dictionary if the test_key defined in
175the node exports section equals the test value. For tests without a preceeding value,
176$[ if exports:test_key == test_value ], a list of nodes which pass the test is returned. For either test
177form the test value can either be a simple value or a node parameter. And as well as an equality test
178a not equals test (!=) can also be used.
179
Andrew Pickfordf4b93c02017-09-13 15:34:10 +0200180**Options**
Andrew Pickfordbb0a6192017-09-08 16:04:24 +0200181
182By default inventory queries only look at nodes in the same environment as the querying node. This can be
183overriden using the +AllEnvs option:
184
185 $[ +AllEnvs exports:test ]
186
187Any errors in rendering the export parameters for a node will give an error for the inventory query as a whole.
188This can be overriden using the +IgnoreErrors option:
189
190 $[ +IgnoreErrors exports:test ]
191
192With the +IgnoreErrors option nodes which generate an error evaluating exports:test will be ignored.
193
194Inventory query options can be combined:
195
196 $[ +AllEnvs +IgnoreErrors exports:test ]
197
198**Logical operators and/or**
199
200The logical operators and/or can be used in inventory queries:
201
202 $[ exports:test_value if exports:test_zero == 0 and exports:test_one == self:value ]
203
204The individual elements of the if statement are evaluated and combined with the logical operators starting from the
205left and working to the right.
Andrew Pickfordf4b93c02017-09-13 15:34:10 +0200206
207
208**Examples**
209
210Defining a cluster of machines using an inventory query, for example to open access to a database server to a
211group of nodes. Given exports/parameters for nodes of the form:
212
213# for all nodes requiring access to the database server
214exports:
215 host:
216 ip_address: aaa.bbb.ccc.ddd
217 cluster: _some_cluster_name_
218
219# for the database server
220parameters:
221 cluster_name: production-cluster
222 postgresql:
223 server:
224 clients: $[ exports:host:ip_address if exports:cluster == self:cluster_name ]
225
226This will generate a dictionary with an entry for node where the export:cluster key for a node is equal to the
227parameter:cluster_name key of the node on which the inventory query is run on. Each entry in the generated dictionary
228will contain the value of the exports:host:ip_address key. The output dictionary (depending on node definitions)
229would look like:
230
231node1:
232 ip_address: aaa.bbb.ccc.ddd
233node2:
234 ip_address: www.xxx.yyy.zzz
235
236For nodes where exports:cluster key is not defined or where the key is not equal to self:cluster_name no entry is made
237in the output dictionary.
238
239In practise the exports:cluster key can be set using a parameter reference:
240
241exports:
242 cluster: ${cluster_name}
243
244parameters:
245 cluster_name: production-cluster
246
247The above exports and parameter definitions could be put into a separate class and then included by nodes which require
248access to the database and included by the database server as well.