The easy intuitive way is to select
the elements equal to an empty string. An other one a bit counter intuitive but more compact to write is to simply reject
elements (which by default will reject all non empty values). The following playbook demonstrate both solutions which give the same result.
Note: the reject
method here will only work as long as other non-empty elements in the list do not evaluate to false. If you ever false boolean values, they will be retained as well
- hosts: localhost
gather_facts: false
vars:
_names: ["","ABC","",""]
blanks_select: "{{ _names | select('==', '') | list }}"
blanks_reject: "{{ _names | reject | list }}"
tasks:
- debug:
var: "{{ item }}"
loop:
- blanks_select
- blanks_reject
Which gives:
PLAY [localhost] *********************************************************************
TASK [debug] *********************************************************************
ok: [localhost] => (item=blanks_select) => {
"ansible_loop_var": "item",
"blanks_select": [
"",
"",
""
],
"item": "blanks_select"
}
ok: [localhost] => (item=blanks_reject) => {
"ansible_loop_var": "item",
"blanks_reject": [
"",
"",
""
],
"item": "blanks_reject"
}
PLAY RECAP *********************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0