如果条件为真,则运行一些包含 yml 文件 [英] If condition is true run some include yml files

查看:25
本文介绍了如果条件为真,则运行一些包含 yml 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些 ubuntucentos 的剧本,我想使用 main.yml 来检查 when: ansible_os_family =='RedHat' 或 ansible_distribution == 'Centos',运行 playbooks(一些 ans many :-)).

I have some playbook for ubuntu and centos and I want to use main.yml to check when: ansible_os_family == 'RedHat' or ansible_distribution == 'Centos', run playbooks ( as some ans many :-) ).

当我跑步时:

-include: centos-xxx.yml
-include: centos-xaa.yml
-include: centos-xsss.yml

它将运行所有这些

基本上我希望剧本在满足条件时运行.

Basically I want that the playbook will run if meet condition.

我没有找到任何说明如何运行包含的文档:如果可能的话,我试图不为每个包含创建任务.

I didn't find any doc that say how to run include: more then one i am trying to not make task per include if possible.

推荐答案

您可以使用 有条件时 以包含文件.事实上,这有点常见.

You can use the when conditional to include files. This is somewhat common, in fact.

- include: centos-xxx.yml
  when: ansible_os_family == 'RedHat' or ansible_distribution == 'Centos'
- include: debian-xxx.yml
  when: ansible_distribution == 'Debian'

根据您的评论-如果您想按顺序运行它们,您有两个选择.这是直截了当的:

Per your comment- if you want to run them in order, you have two options. Here's the straightforward:

- include: centos-a.yml
  when: ansible_os_family == 'RedHat' or ansible_distribution == 'Centos'
- include: centos-b.yml
  when: ansible_os_family == 'RedHat' or ansible_distribution == 'Centos'
- include: centos-c.yml
  when: ansible_os_family == 'RedHat' or ansible_distribution == 'Centos'
- include: centos-d.yml
  when: ansible_os_family == 'RedHat' or ansible_distribution == 'Centos'

或者,您可以这样做:

- include: centos.yml
  when: ansible_os_family == 'RedHat' or ansible_distribution == 'Centos'

centos.yml 中:

- include: centos-a.yml
- include: centos-b.yml
- include: centos-c.yml
- include: centos-d.yml

这篇关于如果条件为真,则运行一些包含 yml 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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