If condition is true run some include yml files

无人久伴 提交于 2020-01-01 08:58:45

问题


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 :-) ).

When I run just:

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

It will run all of them

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.


回答1:


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'

and inside centos.yml:

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


来源:https://stackoverflow.com/questions/29214087/if-condition-is-true-run-some-include-yml-files

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!