Ansible循环

问题描述 投票:1回答:1

我有以下变量:

vars:
  rows:
    - alpha
    - beta
    - gamma
  cols:
    - one
    - two
    - three

我已经想出如何(通过这个网站的帮助)使用反向引用以及如何组合笛卡尔积,我正在做其他任务,但现在我需要做一个“双循环”,最终给我要使用的以下项目序列:

Start -> alpha-one -> alpha-two -> alpha-three
Start -> beta-one -> beta-two -> beta-three
Start -> gamma-one -> gamma-two -> gamma-three

(箭头仅用于说明目的。)

我可以使用default过滤器获取'Start'字符串,我理解基本循环和include_tasks如何用于循环,但我不知道如何在Ansible中“重启”内部循环或如何在循环内循环。

loops ansible
1个回答
0
投票

更新的答案(希望我说得对:)):

我将使用include_tasks将迭代rows列表,并在包含的任务文件中,我将有1个任务为“开始”和第二个任务与cols列表循环:

PB:

---
- hosts: localhost
  gather_facts: false
  vars:
    rows:
    - alpha
    - beta
    - gamma
    cols:
    - one
    - two
    - three

  tasks:
  - name: Loop include tasks
    include_tasks: test2.yml
    with_items:
    - "{{ rows }}"
    loop_control:
      loop_var: rowsval

test2.yml:

---
- name: Start task
  debug:
    msg: "Start iteration"

- name: Loop task
  debug:
    msg: "processing now: {{ rowsval }} with: {{ item }}"
  with_items:
  - "{{ cols }}"

希望能帮助到你。

© www.soinside.com 2019 - 2024. All rights reserved.