How to Use Conditional Statements in Ansible Playbooks
When managing infrastructure with Ansible, conditional statements are a key tool for customizing playbooks. These statements help you control which tasks or roles will run based on specific conditions, like the operating system or server configuration. By using Ansible conditional statements, you can ensure that tasks only run on appropriate hosts, avoiding unnecessary actions and improving efficiency.

Understanding Ansible Conditional Statements
In Ansible playbooks, conditional statements (often using the when keyword) allow you to execute tasks only when certain conditions are met. This is particularly useful when you have a variety of servers with different configurations, and you need to apply specific tasks based on factors like operating system type, hardware, or software versions.
For example, if you want to install Apache only on Ubuntu and CentOS servers, you can use conditional statements to ensure that each task is executed only on the appropriate machine type.
Example of Conditional Statements in Ansible Playbooks
Here’s a simple example of how you can implement conditional statements in an Ansible playbook:
# cat condition.yaml
---
- hosts: all
become: yes
tasks:
- name: Install Apache on Ubuntu
apt:
name=apache2
update_cache=yes
state=latest
when: ansible_os_family == "Debian"
- name: Start Apache on Ubuntu
service:
name=apache2
enabled=yes
state=started
when: ansible_os_family == "Debian"
- name: Install Apache on CentOS
yum:
name=httpd
update_cache=yes
state=latest
when: ansible_os_family == "RedHat"
- name: Start Apache on CentOS
service:
name=httpd
enabled=yes
state=started
when: ansible_os_family == "RedHat"
How the Playbook Works
In the above example, the playbook is designed to install and start Apache only on Ubuntu (Debian family) or CentOS (RedHat family) machines.
- Install Apache on Ubuntu:
This task is executed only when the machine is identified as part of the Debian family (i.e., Ubuntu). - Start Apache on Ubuntu:
Similarly, this task is triggered when the target system is a Debian-based OS. - Install Apache on CentOS:
If the system is running CentOS (RedHat family), this task installs the Apache HTTP server using theyumpackage manager. - Start Apache on CentOS:
This task starts the Apache service on CentOS, but only if the machine is a RedHat-based system.
Running the Playbook
To run this playbook on your hosts, you simply execute the following command:
ansible-playbook condition.yaml
Here’s an example of what the output might look like:
PLAY [all] **************************************************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************************************
ok: [192.168.1.2]
TASK [Install Apache on Ubuntu] *****************************************************************************************************************************
skipping: [192.168.1.2]
TASK [Start Apache on Ubuntu] *******************************************************************************************************************************
skipping: [192.168.1.2]
TASK [Install Apache on CentOS] *****************************************************************************************************************************
changed: [192.168.1.2]
TASK [Start Apache on CentOS] *******************************************************************************************************************************
changed: [192.168.1.2]
PLAY RECAP **************************************************************************************************************************************************
192.168.1.2 : ok=3 changed=2 unreachable=0 failed=0 skipped=2 rescued=0 ignored=0
As you can see, the playbook skips the tasks for Ubuntu nodes and runs the corresponding tasks on CentOS nodes. This demonstrates the power of Ansible conditional statements in optimizing automation tasks.
Why Conditional Statements Matter
Using conditional statements in Ansible not only improves the efficiency of your automation but also reduces the risk of errors. By ensuring that tasks are executed only on suitable systems, you prevent unnecessary changes or installations on servers where they’re not needed.
At the same time, conditional statements make your playbooks more flexible and adaptable, saving you time when managing diverse infrastructure environments.
Integrating ZippyOPS for Enhanced Automation
To streamline your DevOps processes even further, consider integrating ZippyOPS consulting and managed services into your workflow. ZippyOPS specializes in DevOps, DevSecOps, DataOps, and other cloud-based solutions that enhance automation and infrastructure management. With expertise in AIOps, MLOps, and microservices, ZippyOPS can help you implement best practices in automation and security.
By leveraging ZippyOPS services, you can optimize your infrastructure, automate complex workflows, and ensure secure operations across your entire stack. Whether you need assistance with cloud infrastructure, security, or automated operations, ZippyOPS has the experience to accelerate your goals.
For more information, explore ZippyOPS services and solutions:
- ZippyOPS Services
- ZippyOPS Solutions
- ZippyOPS Products
- Watch insightful content on ZippyOPS YouTube
For personalized advice, feel free to reach out to our team at sales@zippyops.com.
Conclusion
Conditional statements are an essential tool for customizing Ansible playbooks. They give you the flexibility to target specific systems, saving time and reducing errors. By integrating ZippyOPS into your automation strategy, you can take your DevOps practices to the next level, ensuring secure and efficient infrastructure management.



