Ansible - Flow (Conditional)

> Infrastructure as code > Ansible

1 - About

3 - Operators

3.1 - If

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

3.2 - When

3.3 - Variable Controled

  • 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

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

5 - Documentation / Reference