如何获得所需位置的组合列表的特定索引值

问题描述

我正在学习ansible,并试图测试ansible TypeError: 'function' object is not iterablezip的配合情况,以便在ansible中获得组合列表的适当索引值。

loop

输出:

下面是预期的组合列表输出。

$ cat   test2_zip_list.yml
---
- name: testing zip with list
  hosts: localhost
  vars:
    nums:
      - 1
      - 2
      - 3
    strs:
      - a
      - b
      - c

  tasks:
  - set_fact:
      num: "{{ item }}"
    loop: "{{ nums | zip(strs) | list }}"

  - name: numa
    debug:
     msg: "{{ num }}"

其他方式

如果我想改变我的比赛并想将某个索引位置的某个值设为。

$ ansible-playbook  test2_zip_list.yml -vvv
--- skipped some portion ----- 
PLAYBOOK: test2_zip_list.yml *************************************************************************************************************************************************************
1 plays in test2_zip_list.yml

PLAY [testing zip with list] *************************************************************************************************************************************************************
META: ran handlers

TASK [set_fact] **************************************************************************************************************************************************************************
task path: /home/user1/ansible_work/test2_zip_list.yml:15
ok: [localhost] => (item=[1,u'a']) => {
    "ansible_facts": {
        "num": [
            1,"a"
        ]
    },"ansible_loop_var": "item","changed": false,"item": [
        1,"a"
    ]
}
ok: [localhost] => (item=[2,u'b']) => {
    "ansible_facts": {
        "num": [
            2,"b"
        ]
    },"item": [
        2,"b"
    ]
}
ok: [localhost] => (item=[3,u'c']) => {
    "ansible_facts": {
        "num": [
            3,"c"
        ]
    },"item": [
        3,"c"
    ]
}

TASK [numa] ******************************************************************************************************************************************************************************
task path: /home/user1/ansible_work/test2_zip_list.yml:19
ok: [localhost] => {
    "msg": [
        3,"c"
    ]
}
META: ran handlers
META: ran handlers

PLAY RECAP *******************************************************************************************************************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

我得到的是..

$ cat  test2_zip_list.yml
---
- name: testing zip with list
  hosts: localhost
  vars:
    nums:
      - 1
      - 2
      - 3
    strs:
      - a
      - b
      - c

  tasks:
  - set_fact:
      num: "{{ item.1 }}"   <-- this is what i given to test 
    loop: "{{ nums | zip(strs) | list }}"

  - name: numa
    debug:
     msg: "{{ num }}"

因此,如果您看到它选择了最后一个合并列表的合并值的第一个索引,即$ ansible-playbook test2_zip_list.yml PLAY [testing zip with list] ************************************************************************************************************************************************************* TASK [set_fact] ************************************************************************************************************************************************************************** ok: [localhost] => (item=[1,u'a']) ok: [localhost] => (item=[2,u'b']) ok: [localhost] => (item=[3,u'c']) TASK [numa] ****************************************************************************************************************************************************************************** ok: [localhost] => { "msg": "c" } PLAY RECAP ******************************************************************************************************************************************************************************* localhost : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 ,那么,我在寻找是否需要获取第一个索引{{1 }}然后如何获得。

解决方法

您的set_fact任务的问题在于,它会迭代nums | zip(strs) | list的元素,并每次都将num设置为该元素。您仅在迭代的第一个元素= 2时才需要使用条件进行值分配。

这是一种实现方法:

  tasks:
  - set_fact:
      my_desired_list: "{{ item }}"
    when: item.0 == 2 or 
    loop: "{{ nums | zip(strs) | list }}"

  - name: numa
    debug:
     var: my_desired_list

如果您想为第二个元素添加等于b的条件,则:

  tasks:
  - set_fact:
      my_desired_list: "{{ item }}"
    when: item.0 == 2 or item.1 == "b"
    loop: "{{ nums | zip(strs) | list }}"

  - name: numa
    debug:
     var: my_desired_list

({or可以根据您的需要and

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...