| 1 |
- name: test with_subelements |
| 2 |
set_fact: "{{ '_'+ item.0.id + item.1 }}={{ item.1 }}" |
| 3 |
with_subelements: |
| 4 |
- "{{element_data}}" |
| 5 |
- the_list |
| 6 |
|
| 7 |
- name: verify with_subelements results |
| 8 |
assert: |
| 9 |
that: |
| 10 |
- "_xf == 'f'" |
| 11 |
- "_xd == 'd'" |
| 12 |
- "_ye == 'e'" |
| 13 |
- "_yf == 'f'" |
| 14 |
|
| 15 |
- name: test with_subelements in subkeys |
| 16 |
set_fact: "{{ '_'+ item.0.id + item.1 }}={{ item.1 }}" |
| 17 |
with_subelements: |
| 18 |
- "{{element_data}}" |
| 19 |
- the.sub.key.list |
| 20 |
|
| 21 |
- name: verify with_subelements in subkeys results |
| 22 |
assert: |
| 23 |
that: |
| 24 |
- "_xq == 'q'" |
| 25 |
- "_xr == 'r'" |
| 26 |
- "_yi == 'i'" |
| 27 |
- "_yo == 'o'" |
| 28 |
|
| 29 |
- name: test with_subelements with missing key or subkey |
| 30 |
set_fact: "{{ '_'+ item.0.id + item.1 }}={{ item.1 }}" |
| 31 |
with_subelements: |
| 32 |
- "{{element_data_missing}}" |
| 33 |
- the.sub.key.list |
| 34 |
- skip_missing: yes |
| 35 |
register: _subelements_missing_subkeys |
| 36 |
|
| 37 |
- debug: var=_subelements_missing_subkeys |
| 38 |
- debug: var=_subelements_missing_subkeys.results|length |
| 39 |
- name: verify with_subelements in subkeys results |
| 40 |
assert: |
| 41 |
that: |
| 42 |
- _subelements_missing_subkeys is not skipped |
| 43 |
- _subelements_missing_subkeys.results|length == 2 |
| 44 |
- "_xk == 'k'" |
| 45 |
- "_xl == 'l'" |
| 46 |
|
| 47 |
# Example from the DOCUMENTATION block |
| 48 |
- set_fact: |
| 49 |
users: |
| 50 |
- name: alice |
| 51 |
authorized: |
| 52 |
- /tmp/alice/onekey.pub |
| 53 |
- /tmp/alice/twokey.pub |
| 54 |
mysql: |
| 55 |
password: mysql-password |
| 56 |
hosts: |
| 57 |
- "%" |
| 58 |
- "127.0.0.1" |
| 59 |
- "::1" |
| 60 |
- "localhost" |
| 61 |
privs: |
| 62 |
- "*.*:SELECT" |
| 63 |
- "DB1.*:ALL" |
| 64 |
groups: |
| 65 |
- wheel |
| 66 |
- name: bob |
| 67 |
authorized: |
| 68 |
- /tmp/bob/id_rsa.pub |
| 69 |
mysql: |
| 70 |
password: other-mysql-password |
| 71 |
hosts: |
| 72 |
- "db1" |
| 73 |
privs: |
| 74 |
- "*.*:SELECT" |
| 75 |
- "DB2.*:ALL" |
| 76 |
- name: carol |
| 77 |
skipped: true |
| 78 |
authorized: |
| 79 |
- /tmp/carol/id_rsa.pub |
| 80 |
mysql: |
| 81 |
password: third-mysql-password |
| 82 |
hosts: |
| 83 |
- "db9" |
| 84 |
privs: |
| 85 |
- "*.*:SELECT" |
| 86 |
- "DB9.*:ALL" |
| 87 |
|
| 88 |
|
| 89 |
- name: Ensure it errors properly with non-dict |
| 90 |
set_fact: |
| 91 |
err: "{{ lookup('subelements', 9001, 'groups', wantlist=true) }}" |
| 92 |
ignore_errors: true |
| 93 |
register: err1 |
| 94 |
|
| 95 |
- assert: |
| 96 |
that: |
| 97 |
- err1 is failed |
| 98 |
- "'first a dict or a list' in err1.msg" |
| 99 |
|
| 100 |
- name: Ensure it errors properly when pointing to non-list |
| 101 |
set_fact: |
| 102 |
err: "{{ lookup('subelements', users, 'mysql.password', wantlist=true) }}" |
| 103 |
ignore_errors: true |
| 104 |
register: err2 |
| 105 |
|
| 106 |
- assert: |
| 107 |
that: |
| 108 |
- err2 is failed |
| 109 |
- "'should point to a list' in err2.msg" |
| 110 |
|
| 111 |
- name: Ensure it properly skips missing keys |
| 112 |
set_fact: |
| 113 |
err: "{{ lookup('subelements', users, 'mysql.hosts.doesnotexist', wantlist=true) }}" |
| 114 |
ignore_errors: true |
| 115 |
register: err3 |
| 116 |
|
| 117 |
- assert: |
| 118 |
that: |
| 119 |
- err3 is failed |
| 120 |
- "'should point to a dictionary' in err3.msg" |
| 121 |
|
| 122 |
- name: Ensure it properly skips missing keys |
| 123 |
set_fact: |
| 124 |
err: "{{ lookup('subelements', users, 'mysql.monkey', wantlist=true) }}" |
| 125 |
ignore_errors: true |
| 126 |
register: err4 |
| 127 |
|
| 128 |
- assert: |
| 129 |
that: |
| 130 |
- err4 is failed |
| 131 |
- >- |
| 132 |
"could not find 'monkey' key in iterated item" in err4.msg |
| 133 |
|
| 134 |
- assert: |
| 135 |
that: |
| 136 |
- "item.0.name != 'carol'" |
| 137 |
with_subelements: |
| 138 |
- "{{ users }}" |
| 139 |
- mysql.privs |
| 140 |
|
| 141 |
- name: Ensure it errors properly when optional arg is nonsensical |
| 142 |
set_fact: |
| 143 |
err: neverset |
| 144 |
with_subelements: |
| 145 |
- "{{ users }}" |
| 146 |
- mysql.privs |
| 147 |
- wolves |
| 148 |
ignore_errors: true |
| 149 |
register: err5 |
| 150 |
|
| 151 |
- assert: |
| 152 |
that: |
| 153 |
- err5 is failed |
| 154 |
- "'the optional third item must be a dict' in err5.msg" |
| 155 |
|
| 156 |
- name: Ensure it errors properly when given way too many args |
| 157 |
set_fact: |
| 158 |
err: neverset |
| 159 |
with_subelements: |
| 160 |
- "{{ users }}" |
| 161 |
- mysql.privs |
| 162 |
- wolves |
| 163 |
- foo |
| 164 |
- bar |
| 165 |
- baz |
| 166 |
- bye now |
| 167 |
ignore_errors: true |
| 168 |
register: err6 |
| 169 |
|
| 170 |
- assert: |
| 171 |
that: |
| 172 |
- err6 is failed |
| 173 |
- "'expects a list of two or three' in err6.msg" |
| 174 |
|
| 175 |
- name: Ensure it errors properly when second arg is invalid type |
| 176 |
set_fact: |
| 177 |
err: neverset |
| 178 |
with_subelements: |
| 179 |
- "{{ users }}" |
| 180 |
- true |
| 181 |
ignore_errors: true |
| 182 |
register: err7 |
| 183 |
|
| 184 |
- assert: |
| 185 |
that: |
| 186 |
- err7 is failed |
| 187 |
- "'second a string' in err7.msg" |
| 188 |
|
| 189 |
- name: Ensure it errors properly when first arg is invalid type |
| 190 |
set_fact: |
| 191 |
err: neverset |
| 192 |
with_subelements: |
| 193 |
- true |
| 194 |
- "{{ users }}" |
| 195 |
ignore_errors: true |
| 196 |
register: err8 |
| 197 |
|
| 198 |
- assert: |
| 199 |
that: |
| 200 |
- err8 is failed |
| 201 |
- "'first a dict or a list' in err8.msg" |
| 202 |
|
| 203 |
- set_fact: |
| 204 |
empty_subelements: "{{ lookup('subelements', {'skipped': true}, 'mysql.hosts', wantlist=true) }}" |
| 205 |
|
| 206 |
- assert: |
| 207 |
that: |
| 208 |
- empty_subelements == [] |
| 209 |
|
| 210 |
- set_fact: |
| 211 |
some_dict: |
| 212 |
key: "{{ users[0] }}" |
| 213 |
another: "{{ users[1] }}" |
| 214 |
|
| 215 |
- name: Ensure it works when we give a dict instead of a list |
| 216 |
set_fact: "user_{{ item.0.name }}={{ item.1 }}" |
| 217 |
with_subelements: |
| 218 |
- "{{ some_dict }}" |
| 219 |
- mysql.hosts |
| 220 |
|
| 221 |
- assert: |
| 222 |
that: |
| 223 |
- "user_alice == 'localhost'" |
| 224 |
- "user_bob == 'db1'" |