Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
List Ansible-如何将一个列表拆分为两个列表?_List_Loops_Ansible - Fatal编程技术网

List Ansible-如何将一个列表拆分为两个列表?

List Ansible-如何将一个列表拆分为两个列表?,list,loops,ansible,List,Loops,Ansible,我搜索并尝试各种方法将一个列表拆分为两个列表。 没有传统的过滤器或任何支持的东西。 有关于将列表合并为一个列表的例子和帖子,但不是相反 我开发了下面的代码,但它不工作,原因很奇怪,列表没有属性0 --- - hosts: localhost gather_facts: no vars: - listA: ['a','b','c','d'] - listB: [] - listC: [] tasks: - block: - debug:

我搜索并尝试各种方法将一个列表拆分为两个列表。 没有传统的过滤器或任何支持的东西。 有关于将列表合并为一个列表的例子和帖子,但不是相反

我开发了下面的代码,但它不工作,原因很奇怪,列表没有属性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: ''
我找到了原因

错误消息“'list object'没有属性u'0'”的问题在于Ansible没有将0识别为数字,但它认为0是字符串。 什么?Ansible如何迭代开始、结束和跨步,并将值存储到“字符串”中我不知道

但问题通过以下代码更新得到解决:

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

感谢techraf在讨论中提出这个问题