我有多个相同长度的数组。我需要遍历长度并调用元素。我尝试了下面的一个,但失败了。你能告诉我我在这方面做错了哪里吗?或者有没有更好的方法来循环数组元素。
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"
我什至尝试了以下方法来代替行:。但是没有运气。
line: "{{ SERIAL_NUMBER[item]int % SERIAL_NUMBER|length }} {{ PASSWORD[item]int % PASSWORD|length }} {{ EXP_DATE[item]int % EXP_DATE|length }}"
我在这里看到 2 个问题。
在花括号 {{- {{ }} }} 内使用花括号调用的数组元素。
- 第二种是在这种情况下使用"何时"。
谢谢。
您可以将query
函数与together
一起使用,以在同一循环迭代中获取每个数组中的第一个元素。
when 语法应为:
when: LICENSE_TYPE == "evaluation"
请检查以下任务(使用debug
模块仅演示query
向循环提供的数据(:
---
- 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) }}"
输出:
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
希望对您有所帮助
你可以试试这个。 祝你好运
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"