Often the result of a block may depend on the value of a variable or previous action result. This topic covers how conditionals are used in recipes.

The when statement

The when statement is responsible for receiving conditional expressions, those that return true or false, that determine if a block will be executed.

  - run: echo This is Linux
    when: os_family == "Linux"

In case of steps:

  - name: 
      # Variable "version_required" will be registered only if the recipe
      # is being running on Linux
      - register_variables:
          version_required: "7.3"
        register: "vars"
    where: os_family == "Linux"

You don’t need to use {{ }} to use variables inside conditionals, as these are already implied.

If a required variable has not been set, you can skip a block:

  - write: "Welcome @{{registered['var']['foo']}}"
    when: registered.has('var')