Ansible Conditional Playbook to install Apache2

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
   - 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

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s