If the condition is true, run some included yml files

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

When I run only:

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

This will run all of them.

Basically, I want the playbook to run on condition of compliance.

I did not find a single document that said that includes include: more than one, I try not to do the task for include, if possible.

+10
source share
1 answer

You can use the condition to include files. This is actually quite common.

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

In your comment - if you want to run them in order, you have two options. Here is simple:

 - 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 
+15
source

Source: https://habr.com/ru/post/984140/


All Articles