问题描述
我正在学习ansible,并试图测试ansible TypeError: 'function' object is not iterable
与zip
的配合情况,以便在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
)