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

查看:25
本文介绍了如何在 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天全站免登陆