-1

Suppose I have playbooks for Nagios installation for both Ubuntu and CentOS machines. How can I call the playbooks depending on the base machine?

I want to load nagios_ubuntu playbook when it is an Ubuntu machine and nagios_centos playbook for CentOS.

techraf
  • 64,883
  • 27
  • 193
  • 198
Ebin Davis
  • 5,421
  • 3
  • 15
  • 20

3 Answers3

0

You can do something like this:

 include: ubuntu.yml
 when: ansible_distribution == "Debian"

 include: centos.yml
 when: ansible_distribution == "Centos"

Be aware of this: http://docs.ansible.com/ansible/latest/playbooks_conditionals.html#applying-when-to-roles-imports-and-includes and of this: http://docs.ansible.com/ansible/latest/include_module.html

  • In Ansible 2.4 you want `import_playbook:` instead of `include:` https://docs.ansible.com/ansible/2.4/playbooks_reuse_includes.html – Henry Finucane Nov 26 '17 at 19:25
0

First of all, you can't include playbook for host. Playbooks have hosts directive defined inside. See playbook/tasks include

So you can have tasks include for different OSes, in this case you may want to use dynamic include statement in your playbook:

- include_tasks: "nagios_{{ ansible_os_family | lower }}.yml"
Konstantin Suvorov
  • 65,183
  • 9
  • 162
  • 193
0

You can utilise an example described in Best Practices section with group_by.

You would still have to modify the playbooks themselves to contain an appropriate hosts declaration, for example:

nagios_centos.yml:

---
- hosts: nagios_centos
# ... the rest of the play

nagios_ubuntu.yml:

---
- hosts: nagios_ubuntu
# ... the rest of the play

Main playbook:

---
- hosts: all
  tasks:
     - group_by:
         key: nagios_{{ ansible_os_family | lower }}

- import_playbook: nagios_centos.yml

- import_playbook: nagios_ubuntu.yml

Because of hosts definition, only one of the playbooks would effectively run.

techraf
  • 64,883
  • 27
  • 193
  • 198