[英]How to compare lists in Ansible?
I have created 2 lists in Ansible:我在 Ansible 中创建了 2 个列表:
{{actual_event_list}}
task1
task2
task3
task4
task5
{{expected_event_list}}
completed
completed
completed
completed
completed
I want to compare both lists in a way that if "task1" == "completed"
then it's a success, else it should fail.我想比较两个列表, if "task1" == "completed"
那么它是成功的,否则它应该失败。 Same goes for all items ( task2 == completed, task3 ==
completed and so on...).所有项目( task2 == completed, task3 ==
completed 等等......)也是如此。
Try map .试试map 。 There might be more options on the filter.过滤器上可能有更多选项。 For example replace例如 替换
shell> cat map-replace.yml
- hosts: localhost
vars:
actual_event_list:
- "{{ task1|default('running') }}"
- "{{ task2|default('running') }}"
- "{{ task3|default('running') }}"
tasks:
- debug:
msg: "{{ actual_event_list|
map('replace', 'completed', 'true')|map('bool')|
list }}"
vars:
task1: completed
task3: completed
gives给
msg:
- true
- false
- true
I would first combine the list to get a dict and then loop over it.我会首先组合列表以获得一个字典,然后循环它。 Here is an example with an assert.这是一个带有断言的示例。
playbook.yml:剧本.yml:
---
- name: Stackoverflow demo
hosts: localhost
tasks:
- name: Set facts
set_fact:
actual_event_list:
- task1
- task2
- task3
- task4
- task5
expected_event_list:
- completed
- completed
- completed
- completed
- completed
# https://docs.ansible.com/ansible/latest/user_guide/complex_data_manipulation.html#id10
- name: Uses 'combine' to update the dictionary and 'zip' to make pairs of both lists
set_fact:
events: "{{ events | default({}) | combine({item[0]: item[1]}) }}"
loop: "{{ (actual_event_list | zip(expected_event_list)) | list }}"
- name: Assert
assert:
that: item.value == "completed"
msg: "{{ item.key }} has status `{{ item.value }}` instead of `completed`"
loop: "{{ events | dict2items }}"
Output: Output:
PLAY [Stackoverflow demo] *************************************************************************************************************************************************************************************************************************
TASK [Gathering Facts] ****************************************************************************************************************************************************************************************************************************
ok: [localhost]
TASK [Set facts] **********************************************************************************************************************************************************************************************************************************
ok: [localhost]
TASK [Uses 'combine' to update the dictionary and 'zip' to make pairs of both lists] **************************************************************************************************************************************************************
ok: [localhost] => (item=['task1', 'completed'])
ok: [localhost] => (item=['task2', 'completed'])
ok: [localhost] => (item=['task3', 'completed'])
ok: [localhost] => (item=['task4', 'completed'])
ok: [localhost] => (item=['task5', 'completed'])
TASK [Assert] *************************************************************************************************************************************************************************************************************************************
ok: [localhost] => (item={'key': 'task1', 'value': 'completed'}) => {
"changed": false,
"item": {
"key": "task1",
"value": "completed"
},
"msg": "All assertions passed"
}
ok: [localhost] => (item={'key': 'task2', 'value': 'completed'}) => {
"changed": false,
"item": {
"key": "task2",
"value": "completed"
},
"msg": "All assertions passed"
}
ok: [localhost] => (item={'key': 'task3', 'value': 'completed'}) => {
"changed": false,
"item": {
"key": "task3",
"value": "completed"
},
"msg": "All assertions passed"
}
ok: [localhost] => (item={'key': 'task4', 'value': 'completed'}) => {
"changed": false,
"item": {
"key": "task4",
"value": "completed"
},
"msg": "All assertions passed"
}
ok: [localhost] => (item={'key': 'task5', 'value': 'completed'}) => {
"changed": false,
"item": {
"key": "task5",
"value": "completed"
},
"msg": "All assertions passed"
}
PLAY RECAP ****************************************************************************************************************************************************************************************************************************************
localhost : ok=4 changed=0 unreachable=0 failed=0
Output when something goes wrong: Output 出现问题时:
PLAY [Stackoverflow demo] *************************************************************************************************************************************************************************************************************************
TASK [Gathering Facts] ****************************************************************************************************************************************************************************************************************************
ok: [localhost]
TASK [Set facts] **********************************************************************************************************************************************************************************************************************************
ok: [localhost]
TASK [Uses 'combine' to update the dictionary and 'zip' to make pairs of both lists] **************************************************************************************************************************************************************
ok: [localhost] => (item=['task1', 'completed'])
ok: [localhost] => (item=['task2', 'completed'])
ok: [localhost] => (item=['task3', 'completed'])
ok: [localhost] => (item=['task4', 'not_completed'])
ok: [localhost] => (item=['task5', 'completed'])
TASK [Assert] *************************************************************************************************************************************************************************************************************************************
ok: [localhost] => (item={'key': 'task1', 'value': 'completed'}) => {
"changed": false,
"item": {
"key": "task1",
"value": "completed"
},
"msg": "All assertions passed"
}
ok: [localhost] => (item={'key': 'task2', 'value': 'completed'}) => {
"changed": false,
"item": {
"key": "task2",
"value": "completed"
},
"msg": "All assertions passed"
}
ok: [localhost] => (item={'key': 'task3', 'value': 'completed'}) => {
"changed": false,
"item": {
"key": "task3",
"value": "completed"
},
"msg": "All assertions passed"
}
failed: [localhost] (item={'key': 'task4', 'value': 'not_completed'}) => {
"assertion": "item.value == \"completed\"",
"changed": false,
"evaluated_to": false,
"item": {
"key": "task4",
"value": "not_completed"
},
"msg": "task4 has status `not_completed` instead of `completed`"
}
ok: [localhost] => (item={'key': 'task5', 'value': 'completed'}) => {
"changed": false,
"item": {
"key": "task5",
"value": "completed"
},
"msg": "All assertions passed"
}
PLAY RECAP ****************************************************************************************************************************************************************************************************************************************
localhost : ok=3 changed=0 unreachable=0 failed=1
You can later enhance that script making sure the list have the same size.您可以稍后增强该脚本以确保列表具有相同的大小。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.