11
votes

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

1 Answers

17
votes

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