Ansible - Flow (Conditional)

Card Puncher Data Processing


This page is about conditional operators in Ansible.



state : {{ "present"
               if (ansible_distribution_release in
                   ([ "wheezy", "jessie", "stretch",
                      "precise", "trusty", "xenial", "bionic" ]))
               else "absent" }}


Ansible - When


See Loop

Variable Controled

variable value can be made conditional.


  • variables by type of operating system with include_vars
- name: Include version-specific variables for CentOS/RHEL.
  include_vars: "RedHat-{{ ansible_distribution_version.split('.')[0] }}.yml"
  when: ansible_distribution == 'CentOS' or
        ansible_distribution == 'Red Hat Enterprise Linux' or
        ansible_distribution == 'RedHat'
  • Install a package for an Os where the package name is not the same by platform
- name: Install mapr-core
    packages_Suse: ['mapr-compat-suse', 'mapr-core', 'mapr-core-internal']
    packages_RedHat: ['mapr-core', 'mapr-core-internal']
    packages_Debian: ['mapr-core', 'mapr-core-internal']
  package: name={{ item }} state=present
  with_items: "{{ vars['packages_' + ansible_os_family] }}"
  register: core_result


Conditional statement that returns a boolean

item.state is undefined
item.state == 'file'
item.state != 'absent'
  • In
ansible_distribution_release in ([ "wheezy", "jessie", "stretch", "precise", "trusty", "xenial", "bionic" ])
  • Logical
and # and
or  # or
(a and b) # grouping

Documentation / Reference

Discover More
Card Puncher Data Processing
Ansible - Function

function in Ansible are tasks that includes other playbook and can pass variable. Include_tasks is done at runtime while import_tasks is done at start time If you have a conditional (such as...
Card Puncher Data Processing
Ansible - Getting Started

This is a getting started guide that shows how to install and where are the training/example. lightbulb ...
Card Puncher Data Processing
Ansible - When

This page is the conditional operator when. all with (loop) clauses get executed BEFORE the when: as the when: can include 'item' to make each iteration of the loop conditional. when is a task property....

Share this page:
Follow us:
Task Runner