質問編集履歴

2

「2020/06/07 追記」 2点目のyml修正

2020/06/07 08:40

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -388,7 +388,7 @@
388
388
 
389
389
  {%- for item in list1 -%}
390
390
 
391
- {%- if (item.Key2 | json_query('[?KeyB!=`abc`]') | length < 0) == True -%}
391
+ {%- if (item.Key2 | json_query('[?KeyB==`abc`]') | length > 0) == False -%}
392
392
 
393
393
  {#- Key2が空ではない場合はabcの文字列がなければリストに追加する -#}
394
394
 
@@ -409,3 +409,21 @@
409
409
  msg: '{{ list2 }}'
410
410
 
411
411
  ```
412
+
413
+
414
+
415
+ ```
416
+
417
+ TASK [debug] ********************************************************************************************
418
+
419
+ ok: [localhost] => {
420
+
421
+ "msg": [
422
+
423
+ "Value1-1"
424
+
425
+ ]
426
+
427
+ }
428
+
429
+ ```

1

2020/06/07 追記

2020/06/07 08:40

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -175,3 +175,237 @@
175
175
  CentOS Linux release 7.8.2003 (Core)
176
176
 
177
177
  ```
178
+
179
+
180
+
181
+
182
+
183
+ ### 2020/06/07 追記
184
+
185
+ ご回答頂いた内容より試してみたPlaybookは以下です。
186
+
187
+ ``` yaml
188
+
189
+ - name: Append List
190
+
191
+ hosts: localhost
192
+
193
+ connection: local
194
+
195
+ gather_facts: False
196
+
197
+ vars:
198
+
199
+ list1: [
200
+
201
+ {
202
+
203
+ "Key1": "Value1-1",
204
+
205
+ "Key2": [
206
+
207
+ {
208
+
209
+ "KeyA": "ValueA-1",
210
+
211
+ "KeyB": "abc"
212
+
213
+ },
214
+
215
+ {
216
+
217
+ "KeyA": "ValueA-2",
218
+
219
+ "KeyB": "defg"
220
+
221
+ }
222
+
223
+ ],
224
+
225
+ "Key3": "Value3-1",
226
+
227
+ },
228
+
229
+ {
230
+
231
+ "Key1": "Value1-2",
232
+
233
+ "Key2": [],
234
+
235
+ "Key3": "Value3-2",
236
+
237
+ },
238
+
239
+ {
240
+
241
+ "Key1": "Value1-3",
242
+
243
+ "Key2": [
244
+
245
+ {
246
+
247
+ "KeyA": "ValueA-2",
248
+
249
+ "KeyB": "defg"
250
+
251
+ }
252
+
253
+ ],
254
+
255
+ "Key3": "Value3-3",
256
+
257
+ }
258
+
259
+ ]
260
+
261
+ tasks:
262
+
263
+ - name: create list2
264
+
265
+ set_fact:
266
+
267
+ list2: >-
268
+
269
+ {%- set tmplist = [] -%}
270
+
271
+ {%- for item in list1 -%}
272
+
273
+ {#- Key2が空の場合はそのままリストに追加 -#}
274
+
275
+ {%- if (item.Key2 | length) <= 0 -%}
276
+
277
+ {%- set dummy = tmplist.append(item.Key1) -%}
278
+
279
+ {%- elif (item.Key2 | json_query('[?KeyB==`abc`]') | length > 0) == False -%}
280
+
281
+ {#- Key2が空ではない場合はabcの文字列がなければリストに追加する -#}
282
+
283
+ {%- set dummy = tmplist.append(item.Key1) -%}
284
+
285
+ {%- endif -%}
286
+
287
+ {%- endfor -%}
288
+
289
+ {{ tmplist }}
290
+
291
+
292
+
293
+ - name: 'debug'
294
+
295
+ debug:
296
+
297
+ msg: '{{ list2 }}'
298
+
299
+ ```
300
+
301
+
302
+
303
+ ``` yaml
304
+
305
+ # 切り分けのためifの分岐を減らす
306
+
307
+ - name: Append List
308
+
309
+ hosts: localhost
310
+
311
+ connection: local
312
+
313
+ gather_facts: False
314
+
315
+ vars:
316
+
317
+ list1: [
318
+
319
+ {
320
+
321
+ "Key1": "Value1-1",
322
+
323
+ "Key2": [
324
+
325
+ {
326
+
327
+ "KeyA": "ValueA-1",
328
+
329
+ "KeyB": "abc"
330
+
331
+ },
332
+
333
+ {
334
+
335
+ "KeyA": "ValueA-2",
336
+
337
+ "KeyB": "defg"
338
+
339
+ }
340
+
341
+ ],
342
+
343
+ "Key3": "Value3-1",
344
+
345
+ },
346
+
347
+ {
348
+
349
+ "Key1": "Value1-2",
350
+
351
+ "Key2": [],
352
+
353
+ "Key3": "Value3-2",
354
+
355
+ },
356
+
357
+ {
358
+
359
+ "Key1": "Value1-3",
360
+
361
+ "Key2": [
362
+
363
+ {
364
+
365
+ "KeyA": "ValueA-2",
366
+
367
+ "KeyB": "defg"
368
+
369
+ }
370
+
371
+ ],
372
+
373
+ "Key3": "Value3-3",
374
+
375
+ }
376
+
377
+ ]
378
+
379
+ tasks:
380
+
381
+ - name: create list2
382
+
383
+ set_fact:
384
+
385
+ list2: >-
386
+
387
+ {%- set tmplist = [] -%}
388
+
389
+ {%- for item in list1 -%}
390
+
391
+ {%- if (item.Key2 | json_query('[?KeyB!=`abc`]') | length < 0) == True -%}
392
+
393
+ {#- Key2が空ではない場合はabcの文字列がなければリストに追加する -#}
394
+
395
+ {%- set dummy = tmplist.append(item.Key1) -%}
396
+
397
+ {%- endif -%}
398
+
399
+ {%- endfor -%}
400
+
401
+ {{ tmplist }}
402
+
403
+
404
+
405
+ - name: 'debug'
406
+
407
+ debug:
408
+
409
+ msg: '{{ list2 }}'
410
+
411
+ ```