Using Ansible conditionals
Conditionals are a powerful functionality of all programming languages. Ansible has the ability to use conditional logic to run tasks/plays on a given host based on a conditional test. In this recipe, we will demonstrate how to use conditional task execution.
Here, we will adapt the previous playbook and add two conditionals:
- The first task runs only when the host is in the
linuxAnsible group - The second task runs when the host is in the
network_equipmentparent group
Very intuitively, the fundamental conditional statement in Ansible is the when condition: a task will only execute when the conditional is true. To meet the new requirements, the original playbook has been reused and extended:
root@cookbook-lab:~/cookbook-lab/chapters/ch02# cp pb_debug_vars.yml pb_debug_conditionals.yml
root@cookbook-lab:~/cookbook-lab/chapters/ch02# vi pb_debug_conditionals.yml
---
- name: Debug with...