如何在Ansible中执行交替角色? [英] How to execute alternating roles in Ansible?

查看:68
本文介绍了如何在Ansible中执行交替角色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要以某种方式遍历变量列表,并为每次迭代一次执行以下两个角色,每次迭代将变量传递给角色.例如,给定变量列表100-101,我需要按role1:100,role2:100,role1:101,role2:101的顺序执行.变量100-100应该传递给角色内部的任务.

I need to somehow loop over a list of variables and execute both of the below roles once for each iteration, on each iteration passing a variable to the role. For example, given a variable list of 100-101, I need to execute in the order role1:100, role2:100, role1:101, role2:101. The variables 100-100 should be passed to the tasks inside the role.

---
- hosts: group1
  tasks:
  - include_role:
      name: role1

- hosts: group2
  tasks:
  - include_role:
      name: role2

我一直在寻找以下答案作为可能的解决方案,但是我不确定如何使其适应我的需求.可以在Ansible中完成上述方案吗?

I was looking at the below answer as a possible solution but I am not sure how to adapt it to my needs. Can the above scenario be accomplished in Ansible?

Ansible:如何使用数组?

推荐答案

遍历变量和角色名称的笛卡尔积:

Loop over the Cartesian product of variables and role names:

vars:
  roles_to_include:
    - role1
    - role2
  values_to_pass:
    - 100
    - 101

tasks:
  - include_role:
      name: "{{ item.1 }}"
    vars:
      my_variable: "{{ item.0 }}"
    loop: "{{ values_to_pass | product(roles_to_include) | list }}"

这篇关于如何在Ansible中执行交替角色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆