循环使用多个子元素的两个版本

Loop over two versions with multiple subelements

我正在努力减少在将服务版本发布到我们的存储库时我们必须为某些剧本重复的任务量。我想要做的是发布最新版本“最新”和我们使用 set_fact 的版本,并循环遍历我们定义的所有服务。

电流示例:

  - name: Push Helm Template to ACR
    command: helm chart push {{ docker_registry }}/helm/{{ service_name }}:{{ item }}
    loop:
      - latest
      - "{{ version }}"

我想做的是让它发布

的所有最新版本
- service1
- service2
- service3

等等

并在同一任务中,使用相同的循环发布该服务的 {{ version }}。这可以在一项任务中完成还是将任务分开更好?

product filter。简而言之:

- hosts: localhost
  gather_facts: false

  vars:
    version_list:
      - latest
      - "{{ version }}"

    services:
      - service1
      - service2
      - service3

    docker_registry: https://my.reg.com

  tasks:
    - name: set version
      set_fact:
        version: a.b.c

    - name: Show how to use with a debug task
      debug:
        msg: "Command is:
          helm chart push {{ docker_registry }}/helm/{{ item.0 }}:{{ item.1 }}"
      loop: "{{ services | product(version_list) }}"

给出:

PLAY [localhost] *****************************************************************************

TASK [set version] *****************************************************************************
ok: [localhost]

TASK [Show how to use with a debug task] *****************************************************************************
ok: [localhost] => (item=['service1', 'latest']) => {
    "msg": "Command is: helm chart push https://my.reg.com/helm/service1:latest"
}
ok: [localhost] => (item=['service1', 'a.b.c']) => {
    "msg": "Command is: helm chart push https://my.reg.com/helm/service1:a.b.c"
}
ok: [localhost] => (item=['service2', 'latest']) => {
    "msg": "Command is: helm chart push https://my.reg.com/helm/service2:latest"
}
ok: [localhost] => (item=['service2', 'a.b.c']) => {
    "msg": "Command is: helm chart push https://my.reg.com/helm/service2:a.b.c"
}
ok: [localhost] => (item=['service3', 'latest']) => {
    "msg": "Command is: helm chart push https://my.reg.com/helm/service3:latest"
}
ok: [localhost] => (item=['service3', 'a.b.c']) => {
    "msg": "Command is: helm chart push https://my.reg.com/helm/service3:a.b.c"
}

PLAY RECAP *****************************************************************************
localhost: ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0