Skip to content

Example 6

hosts.yml

centos:
  hosts:
    provisioner:
      ansible_connection: local
    centos1:
      ansible_host: '192.168.77.22'
ubuntu:
  hosts:
    ubuntu1:
      ansible_host: '192.168.77.23'

playbook.yml

- name: Example playbook
  hosts: all
  become: true
  tasks:
    - name: Install vim on CentOS machines
      yum:
       name: vim
       state: present
      when: ansible_distribution == "CentOS"

Tip

Ansible Playbooks support conditional clauses. The when directive stores the conditions in string or list form.

Execute the following command

ansible-playbook playbook.yml

What is the expected output of the command?

The changes will take place on provisioner and centos1, whereas ubuntu1 will get skipped since it does not match the conditional clause.