Ansible循环:如何使用with_sequence为索引

时间:2018-05-23 20:33:56

标签: arrays loops ansible .when ansible-template

我有多个长度相同的数组。我需要循环遍历长度并调用元素。我试过下面的一个但失败了。能告诉我这里我做错了吗?或者是否有更好的方法来循环数组元素。

  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个问题。

  1. 在花括号{{{{}}}}。
  2. 内使用花括号调用的数组元素
  3. 第二种是在这种情况下使用“when”。
  4. 谢谢。

2 个答案:

答案 0 :(得分:1)

你可以使用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 

希望有所帮助

答案 1 :(得分:0)

您可以尝试一下。祝你好运

  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"