ansible.builtin.ternary filter – Ternary operation filter
Note
This filter plugin is part of ansible-core
and included in all Ansible
installations. In most cases, you can use the short
plugin name
ternary
.
However, we recommend you use the Fully Qualified Collection Name (FQCN) ansible.builtin.ternary
for easy linking to the
plugin documentation and to avoid conflicting with other collections that may have
the same filter plugin name.
Synopsis
Return the first value if the input is
True
, the second ifFalse
.
Input
This describes the input of the filter, the value before | ansible.builtin.ternary
.
Parameter |
Comments |
---|---|
A boolean expression, must evaluate to Choices:
|
Positional parameters
This describes positional parameters of the filter. These are the values positional1
, positional2
and so on in the following
example: input | ansible.builtin.ternary(positional1, positional2, ...)
Parameter |
Comments |
---|---|
Value to return if the input is |
|
Value to return if the input is |
Keyword parameters
This describes keyword parameters of the filter. These are the values key1=value1
, key2=value2
and so on in the following
example: input | ansible.builtin.ternary(key1=value1, key2=value2, ...)
Parameter |
Comments |
---|---|
Value to return if the input is |
Notes
Note
When keyword and positional parameters are used together, positional parameters must be listed before keyword parameters:
input | ansible.builtin.ternary(positional1, positional2, key1=value1, key2=value2)
Vars as values are evaluated even when not returned. This is due to them being evaluated before being passed into the filter.
Examples
# set first 10 volumes rw, rest as dp
volume_mode: "{{ (item|int < 11)|ternary('rw', 'dp') }}"
# choose correct vpc subnet id, note that vars as values are evaluated even if not returned
vpc_subnet_id: "{{ (ec2_subnet_type == 'public') | ternary(ec2_vpc_public_subnet_id, ec2_vpc_private_subnet_id) }}"
- name: service-foo, use systemd module unless upstart is present, then use old service module
service:
state: restarted
enabled: yes
use: "{{ (ansible_service_mgr == 'upstart') | ternary('service', 'systemd') }}"
Return Value
Key |
Description |
---|---|
The value indicated by the input. Returned: success |