I am learning ansible and tried to test how ansible zip
works well with loop
in order to get the appropriate index value of the combined list in 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 }}"
Output:
Below is the combined list output which is as expected..
$ 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
Other way
if i want to change my play and want to certain value of certain index position..
$ 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 }}"
What i got is below ..
$ 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
So, if you will see its picked the first index of the combined value of list of the last combine ie item=[3, u'c']
, So, i'm looking in case i need to get the first index of (item=[2, u'b'])
then how to get that.
the issue with your
set_fact
task is that it iterates of the elements ofnums | zip(strs) | list
and sets thenum
to the element each time. you need to use a condition to do the value assignment only when the iteration has first element = 2.here is a way to do it:
if you want to add a condition for 2nd element to be equal to
b
then:(
or
can beand
depending or your needs)