简体   繁体   中英

How to compare lists in Ansible?

I have created 2 lists in Ansible:

{{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. Same goes for all items ( task2 == completed, task3 == completed and so on...).

Try 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:

---

- 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 when something goes wrong:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM