2017-08-15 55 views
0

我搜索並試圖用各種方法將列表分成兩個列表。 沒有傳統的過濾器或任何支持。 有一些關於將列表合併成一個的例子和文章,但沒有其他方法。Ansible - 如何將列表分成兩個列表?

我在代碼下面開發,但它不工作,很奇怪的原因,列表沒有屬性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 

這是試運行結果與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: '' 
+0

https://github.com/ansible/ansible/issues/17852 – techraf

+0

感謝techraf爲把它提出來討論 – user3376413

回答

0

我找到了原因。

有關錯誤消息「'列表對象'沒有屬性u'0'」的問題是,Ansible沒有將0識別爲數字,但它認爲0是字符串。 什麼? Ansible如何迭代開始,結束並跨度並將值存儲到「String」中? - 我不知道。

但與下面的代碼更新解決了這個問題:

--- 
- 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|int] ] }}" 
    with_sequence: start=0 end=3 stride=2 
    - set_fact: 
     listC: "{{ listC + [ listA[item|int] ] }}" 
    with_sequence: start=1 end=3 stride=2 

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

,其結果是:

$ 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] **************************************************************** 
ok: [localhost] => (item=0) 
ok: [localhost] => (item=2) 

TASK [set_fact] **************************************************************** 
ok: [localhost] => (item=1) 
ok: [localhost] => (item=3) 

TASK [debug] ******************************************************************* 
ok: [localhost] => { 
    "listA": [ 
     "a", 
     "b", 
     "c", 
     "d" 
    ] 
} 

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

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

PLAY RECAP ********************************************************************* 
localhost     : ok=8 changed=0 unreachable=0 failed=0