簡體   English   中英

如何比較 Ansible 中的列表?

[英]How to compare lists in Ansible?

我在 Ansible 中創建了 2 個列表:

{{actual_event_list}}  
task1  
task2  
task3  
task4  
task5
{{expected_event_list}}  
completed  
completed  
completed  
completed  
completed  

我想比較兩個列表, if "task1" == "completed"那么它是成功的,否則它應該失敗。 所有項目( task2 == completed, task3 == completed 等等......)也是如此。

試試map 過濾器上可能有更多選項。 例如 替換

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

  msg:
  - true
  - false
  - true

我會首先組合列表以獲得一個字典,然后循環它。 這是一個帶有斷言的示例。

劇本.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:

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 出現問題時:

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   

您可以稍后增強該腳本以確保列表具有相同的大小。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM