Ansible:使用循环变量的序列范围

Ansible:使用循环变量的序列范围,ansible,Ansible,我需要根据名称和包含的变量创建一定数量的systemd unit文件,其中包含要创建的文件数,如: app-name@1.service app-name@2.service app-name@3.service script-name@1.service script-name@2.service script-name@3.service script-name@4.service 它使用range函数处理嵌套循环,但我不知道如何在range()参数中使用循环变量(item.1.work

我需要根据名称和包含的变量创建一定数量的systemd unit文件,其中包含要创建的文件数,如:

app-name@1.service
app-name@2.service
app-name@3.service

script-name@1.service
script-name@2.service
script-name@3.service
script-name@4.service
它使用range函数处理嵌套循环,但我不知道如何在range()参数中使用循环变量(item.1.workers)

- hosts: localhost
  vars:
    apps:
        - name: app-name
          workers: 3
        - name: script-name
          workers: 5
  connection: local
  tasks:

  - name: test
    debug:
      msg: "{{ item.1.name }}@{{ item.0 }}.service"
    loop: "{{ range(1, 3) | product(apps) | list }}"
让我们在第一个任务中创建工人列表,并在第二个任务中使用子元素循环

- set_fact:
    apps1: "{{ apps1|default([]) +
               [{'name': item.name,
                 'workers': range(1, item.workers + 1)|list}] }}"
  loop: "{{ apps }}"
- debug:
    msg: "{{ item.0.name }}@{{ item.1 }}.service"
  loop: "{{ apps1|subelements('workers') }}"
给予

  msg: app-name@1.service
  msg: app-name@2.service
  msg: app-name@3.service
  msg: script-name@1.service
  msg: script-name@2.service
  msg: script-name@3.service
  msg: script-name@4.service
  msg: script-name@5.service