Ansible - how to split a list into two lists?

Viewed 5413

I searched and tried every way to split a list into two lists. There were no conventional filters or anything supported. There were examples and posts about combining lists into one, but not the other way around.

I developed below code but it does not work with very strange reason, list does not have attribute 0?

---
- hosts: localhost
  gather_facts: no
  vars:
    - listA: ['a','b','c','d']
    - listB: []
    - listC: []

  tasks:
  - block:
      - debug:
          var: listA[0]
      - debug:
          var: listB
      - debug:
          var: listC
  - set_fact:
      listB: "{{ listB + [listA[item]] }}"
    with_sequence: start=0 end=3 stride=2
  - set_fact:
      listC: "{{ listC + [listA[item]] }}"
    with_sequence: start=1 end=3 stride=2

  - block:
      - debug:
          var: listA
      - debug:
          var: listB
      - debug:
          var: listC

This is the test run outcome with Ansible 2.1.1.0

$ ansible-playbook test_sequenceeasy.yml
[WARNING]: log file at '{{planfile | dirname}}/AnsibleLog.txt' is not writeable and we cannot create it, aborting


PLAY [localhost] ***************************************************************

TASK [debug] *******************************************************************
ok: [localhost] => {
    "listA[0]": "a"
}

TASK [debug] *******************************************************************
ok: [localhost] => {
    "listB": []
}

TASK [debug] *******************************************************************
ok: [localhost] => {
    "listC": []
}

TASK [set_fact] ****************************************************************
fatal: [localhost]: FAILED! => {"failed": true, "msg": "the field 'args' has an invalid value, which appears to include a variable that is undefined. The error was: 'list object' has no attribute u'0'\n\nThe error appears to have been in '/apps/infra/Tools/Ansible_WLNMiddleware/test_sequenceeasy.yml': line 17, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n          var: listC\n  - set_fact:\n    ^ here\n"}

NO MORE HOSTS LEFT *************************************************************
 [WARNING]: Could not create retry file 'test_sequenceeasy.retry'.         [Errno 2] No such file or directory: ''
1 Answers
Related