Ansible - Flow (Conditional)

Card Puncher Data Processing

About

This page is about conditional operators in Ansible.

Operators

If

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

When

Ansible - When

Loop

See Loop

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





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