Ansible - Flow (Conditional)

About

Operators

If

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

When

Variable Controled

variable value can be made conditional.

Example:

  • 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
  vars:
    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

Expression

Conditional statement that returns a boolean

logrotate__enabled|bool
item.filename|d()
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


Powered by ComboStrap