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


Categories: Ansible, DevOps

Tags: , , , ,

Leave a Reply

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

WordPress.com Logo

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

Google photo

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

Twitter picture

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

Facebook photo

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

Connecting to %s

%d bloggers like this: