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

How to get the particular index value of a combined list for a desired position ansible

我正在学习 ansible 并尝试测试 ansible zip 如何与 loop 一起工作,以便在 ansible 中获得组合列表的适当索引值。

$ 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"
        ]
    },
    "ansible_loop_var": "item",
    "changed": false,
    "item": [
        2,
        "b"
    ]
}
ok: [localhost] => (item=[3, u'c']) => {
    "ansible_facts": {
        "num": [
            3,
            "c"
        ]
    },
    "ansible_loop_var": "item",
    "changed": false,
    "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

因此,如果您看到它选择了最后一个组合列表的组合值的第一个索引,即 item=[3, u'c'],那么,我正在寻找以防万一我需要获取 (item=[2, u'b']) 那么如何获得。

您的 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 取决于您的需要)