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

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

问题描述

我有一些用于ubuntucentos的剧本,并且我想使用main.yml检查when: ansible_os_family == 'RedHat' or ansible_distribution == 'Centos',运行剧本(有些甚至很多:-)).

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.

我没有找到任何说明如何运行include的文档:如果可能的话,我尝试不让更多文档包含在内.

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'

或者,您可以执行以下操作:

Or, you can do this:

- 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天全站免登陆