Executing roles conditionally
The Nginx role that we created earlier to set up web server supports only Debian-based systems. Running this logic on other systems could lead to failure. For example, the Nginx role uses the apt
module to install packages, which would not work on RedHat-based systems, which depend on the yum
package manager. This could be avoided by adding the when
statement with a fact to selectively execute based on an OS family. The following is the snippet from the www.yml
playbook:
#filename: www.yml (snippet) - hosts: www roles: - { role: nginx, when: ansible_os_family == 'Debian' }
Get Ansible Playbook Essentials now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.