Andrew Pickford | bb0a619 | 2017-09-08 16:04:24 +0200 | [diff] [blame] | 1 | Escaping of References and Inventory Queries |
| 2 | ============================================ |
| 3 | |
| 4 | Reference and inventory queries can be escaped to produce literal strings, for example: |
| 5 | |
| 6 | parameters: |
| 7 | colour: Blue |
| 8 | unescaped: The colour is ${colour} |
| 9 | escaped: The colour is \${colour} |
| 10 | double_escaped: The colour is \\${colour} |
| 11 | |
| 12 | |
| 13 | This would produce: |
| 14 | |
| 15 | parameters: |
| 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 | |
| 23 | Merging Referenced Lists and Dictionaries |
| 24 | ========================================= |
| 25 | |
| 26 | Referenced lists or dicts can now be merged: |
| 27 | |
| 28 | # nodes/test.yml |
| 29 | classes: |
| 30 | - test1 |
| 31 | - test2 |
| 32 | parameters: |
| 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 |
| 43 | parameters: |
| 44 | three: ${one} |
| 45 | |
| 46 | # classes/test2.yml |
| 47 | parameters: |
| 48 | three: ${two} |
| 49 | |
| 50 | running reclass.py --nodeinfo node1 then gives: |
| 51 | |
| 52 | parameters: |
| 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 | |
| 66 | This first sets the parameter three to the value of parameter one (class test1) then merges parameter two into |
| 67 | parameter three (class test2) and finally merges the parameter three definition given in the node definition into |
| 68 | |
| 69 | |
| 70 | |
| 71 | Nested References |
| 72 | ================= |
| 73 | |
| 74 | References can now be nested, for example: |
| 75 | |
| 76 | # nodes/node1.yml |
| 77 | parameters: |
| 78 | alpha: |
| 79 | one: ${beta:${alpha:two}} |
| 80 | two: a |
| 81 | beta: |
| 82 | a: 99 |
| 83 | |
| 84 | reclass.py --nodeinfo node1 then gives: |
| 85 | |
| 86 | parameters: |
| 87 | alpha: |
| 88 | one: 99 |
| 89 | two: a |
| 90 | beta: |
| 91 | a: 99 |
| 92 | |
| 93 | The ${beta:${alpha:two}} construct first resolves the ${alpha:two} reference to the value 'a', then resolves |
| 94 | the reference ${beta:a} to the value 99. |
| 95 | |
| 96 | |
| 97 | |
| 98 | Inventory Queries |
| 99 | ================= |
| 100 | |
| 101 | Inventory 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 |
| 104 | exports: |
| 105 | test_zero: 0 |
| 106 | test_one: |
| 107 | name: ${name} |
| 108 | value: 6 |
| 109 | test_two: ${dict} |
| 110 | |
| 111 | parameters: |
| 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 |
| 122 | exports: |
| 123 | test_zero: 0 |
| 124 | test_one: |
| 125 | name: ${name} |
| 126 | value: 7 |
| 127 | test_two: ${dict} |
| 128 | |
| 129 | parameters: |
| 130 | name: node2 |
| 131 | dict: |
| 132 | a: 11 |
| 133 | b: 22 |
| 134 | |
| 135 | |
| 136 | running reclass.py --nodeinfo node1 gives (listing only the exports and parameters): |
| 137 | |
| 138 | exports: |
| 139 | test_one: |
| 140 | name: node1 |
| 141 | value: 6 |
| 142 | test_two: |
| 143 | a: 1 |
| 144 | b: 2 |
| 145 | parameters: |
| 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 | |
| 170 | Exports defined for a node can be a simple value or a reference to a parameter in the node definition. |
| 171 | The $[] inventory queries are calculated for simple value expressions, $[ exports:key ], by returning |
| 172 | a dictionary with an element ({ node_name: key value }) for each node which defines 'key' in the exports |
| 173 | section. For tests with a preceeding value, $[ exports:key if exports:test_key == test_value ], the |
| 174 | element ({ node_name: key value }) is only added to the returned dictionary if the test_key defined in |
| 175 | the 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 |
| 177 | form the test value can either be a simple value or a node parameter. And as well as an equality test |
| 178 | a not equals test (!=) can also be used. |
| 179 | |
| 180 | **Inventory query options** |
| 181 | |
| 182 | By default inventory queries only look at nodes in the same environment as the querying node. This can be |
| 183 | overriden using the +AllEnvs option: |
| 184 | |
| 185 | $[ +AllEnvs exports:test ] |
| 186 | |
| 187 | Any errors in rendering the export parameters for a node will give an error for the inventory query as a whole. |
| 188 | This can be overriden using the +IgnoreErrors option: |
| 189 | |
| 190 | $[ +IgnoreErrors exports:test ] |
| 191 | |
| 192 | With the +IgnoreErrors option nodes which generate an error evaluating exports:test will be ignored. |
| 193 | |
| 194 | Inventory query options can be combined: |
| 195 | |
| 196 | $[ +AllEnvs +IgnoreErrors exports:test ] |
| 197 | |
| 198 | **Logical operators and/or** |
| 199 | |
| 200 | The 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 | |
| 204 | The individual elements of the if statement are evaluated and combined with the logical operators starting from the |
| 205 | left and working to the right. |