Understanding Ansible conditions, handlers, and loops
In this part of the chapter, we will look at some of the advanced features in the Ansible playbook.
Designing conditions
An Ansible playbook can execute tasks (or skip them) based on the results of specific conditions inside the task—for example, when you want to install packages on a specific family of operating systems (Debian or CentOS), or when the operating system is a particular version, or even when the remote hosts are virtual, not bare metal. This can be done by using the when
clause inside of the task.
Let's enhance the previous playbook and limit the openssh-server
installation to only CentOS based systems, so that it does not give an error when it hits an Ubuntu server that uses the apt
module, not yum
.
First, we will add the following two sections to our inventory
file, to group the CentOS and Ubuntu machines in the infra
section:
[infra:children] centos-servers ubuntu-servers [infra:vars] Ansible_ssh_user=root Ansible_ssh_pass...