In this post we will be seeing how to use “when” conditional operator to install Apache2 on Debian and RedHat family.
You can read more about the when operator here.
So let’ s get started, Create a sample playbook named conditional.yml and write below content in it.
--- # conditional - hosts: all become: true connection: ssh tasks: - name: install apache server on debian command: apt-get -y install apache2 when: ansible_os_family == 'Debian' - name: install apache server for redhat command: yum -y install httpd when: ansible_os_family == 'RedHat'
Now run this playbook using below command.
ansible-playbook -i inventory conditional.yml