Ansible: Know When to Say When

Conditional execution is the powerhouse of every programming language. Ansible provides the when operator to make run-time decisions about whether or not a play should be executed.

Ansible’s when statement is analogous to the if statement found in many common programming languages. If the condition tested by the when statement passes (evaluates to True), the play is executed, otherwise it will be skipped.

Conditionals may be written to test against the values of ansible facts gathered at runtime, local facts, local variables, playbook variables, or extra variables passed in via the command line.

Tests with the when statement fall into 4 categories:

  • Tests for the existence of a variable
  • Tests against the boolean value of a variable
  • Tests comparing integers, and
  • Test comparing strings

An example of a test for a variable’s existence could be written to check if a default gateway has been set for each managed host, and print it’s value if it has:

  - name: If gateway is defined print its address
      msg: Gateway is {{ ansible_default_ipv4['gateway'] }}
    when: ansible_default_ipv4['gateway'] is defined

The is defined operator returns True if the variable is defined. It does not check the boolean value of the variable. The operation can be negated with the operator is not defined.

Boolean value tests for a boolean set to True need no operator and can simply be shorthanded to the variable being tested, for example this when statement:

- name: Check for RHGB enabled
    msg: "RHGB is enabled on {{ inventory_hostname }}"
  when: ansible_facts['cmdline']['rhgb']

returns true when the ansible fact indicating that Red Hat Graphical Boot is enabled is set to True. To check if a variable is set to false, append not to the when statement ahead of the variable.

when: not ansible_facts['cmdline']['rhgb']

String and variable comparison operators take the familiar form of == for equality, != for not equal to, etc. A full listing of playbook conditional operators can be found in Ansible’s official documentation at:

Comparison tests may also be chained together with the logical operators and & or.

For example, the following play will install the Apache http server if and only if the managed host is running RedHat 8.

 - name: Install httpd when distribution is Redhat and major version is 8
     name: httpd
     state: latest
   when: >
     ansible_distribution_major_version == "8" and
     ansible_distribution == "RedHat"


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.