Ansible Filter default

From NovaOrdis Knowledge Base
Revision as of 05:25, 5 July 2021 by Ovidiu (talk | contribs) (→‎Overview)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Internal

Overview

The filter provides a default value in case an undefined variable.

- name: Debug
  debug:
    msg: "value of {{ color | default('blue') }}"

In case a variable evaluates to false or to an empty string, and we want to provide a default value in this situation, default() filter needs a second parameter with the value true:

- name: Debug
  debug:
    msg: "{{ lookup('env', 'MY_USER') | default('admin', true) }}"

Also see:

Playground

https://github.com/ovidiuf/playground/blob/master/ansible/filters/default-value-for-variable/playbook.yaml