Reputation: 101
I have multiple arrays with same length. I need to loop through the length and call the elements. I tried the below one and failed. Can you please let me know where I did wrong in this. Or is there any better approach to loop through array elements.
tasks:
- name: Set facts
set_fact:
SERIAL_NUMBER: ['2342', '4455', '5643']
PASSWORD: ['xxx', 'yyy', 'zzz']
EXP_DATE: ['06-10-18', '07-01-19', '06-01-18']
LICENSE_TYPE: "evaluation"
- name: Execute the script to apply evaluation license
lineinfile:
dest: "/root/test.txt"
line: "{{ SERIAL_NUMBER[{{ item }}] }} {{ PASSWORD[{{ item }}] }} {{ EXP_DATE[{{ item }}] }}"
create: yes
with_sequence: start=0 end={{ SERIAL_NUMBER|length }}
when: "{{ LICENSE_TYPE }}" == "evaluation"
I even tried the below approach in place of line:. But no luck.
line: "{{ SERIAL_NUMBER[item]int % SERIAL_NUMBER|length }} {{ PASSWORD[item]int % PASSWORD|length }} {{ EXP_DATE[item]int % EXP_DATE|length }}"
I see 2 issues here.
Thank you.
Upvotes: 2
Views: 7141
Reputation: 1
You can try this. good luck
tasks:
- name: Set facts
set_fact:
SERIAL_NUMBER: ['2342', '4455', '5643']
PASSWORD: ['xxx', 'yyy', 'zzz']
EXP_DATE: ['06-10-18', '07-01-19', '06-01-18']
LICENSE_TYPE: "evaluation"
- name: Execute the script to apply evaluation license
lineinfile:
dest: "/root/test.txt"
line: "{{ SERIAL_NUMBER[index] }} {{ PASSWORD[index] }} {{ EXP_DATE[index] }}"
create: yes
loop: "{{ SERIAL_NUMBER|flatten(levels=1) }}"
loop_control:
index_var: index
when: "{{ LICENSE_TYPE }}" == "evaluation"
Upvotes: 0
Reputation: 6705
you can use the query
function with together
to get in the same loop iteration the 1st element from each array.
the when syntax should be:
when: LICENSE_TYPE == "evaluation"
please check the below task (with debug
module to just demonstrate the data query
supplies to the loop):
---
- hosts: localhost
connection: local
gather_facts: false
vars:
tasks:
- name: Set facts
set_fact:
SERIAL_NUMBER: ['2342', '4455', '5643']
PASSWORD: ['xxx', 'yyy', 'zzz']
EXP_DATE: ['06-10-18', '07-01-19', '06-01-18']
LICENSE_TYPE: "evaluation"
- name: Execute the script to apply evaluation license
debug:
msg: "SERIAL_NUMBER: {{ item[0] }}, PASSWORD: {{ item[1] }}, EXP_DATE: {{ item[2] }}"
when: LICENSE_TYPE == "evaluation"
loop: "{{ query('together', SERIAL_NUMBER, PASSWORD, EXP_DATE) }}"
output:
PLAY [localhost] ****************************************************************************************************************************************************************************************************
TASK [Set facts] ****************************************************************************************************************************************************************************************************
ok: [localhost]
TASK [Execute the script to apply evaluation license] ***************************************************************************************************************************************************************
ok: [localhost] => (item=None) => {
"msg": "SERIAL_NUMBER: 2342, PASSWORD: xxx, EXP_DATE: 06-10-18"
}
ok: [localhost] => (item=None) => {
"msg": "SERIAL_NUMBER: 4455, PASSWORD: yyy, EXP_DATE: 07-01-19"
}
ok: [localhost] => (item=None) => {
"msg": "SERIAL_NUMBER: 5643, PASSWORD: zzz, EXP_DATE: 06-01-18"
}
PLAY RECAP **********************************************************************************************************************************************************************************************************
localhost
hope it helps
Upvotes: 1