Skip to content

ternary

Collection Note

This module is part of the ansible.builtin collection. To install the collection, use:

ansible-galaxy collection install ansible.builtin
Added in version 1.9.

Synopsis

  • Return the first value if the input is V(True), the second if V(False).

Parameters

Parameter Defaults / Choices Comments
_input
bool
required
A boolean expression, must evaluate to V(True) or V(False).
false_val
any
Value to return if the input is V(False).
none_val
any
Value to return if the input is V(None). If not set, V(None) will be treated as V(False).
Version Added: 2.8
true_val
any
required
Value to return if the input is V(True).

Notes

Note

  • 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 Values

Key Data Type Description Returned
_value any The value indicated by the input.

Authors

  • Brian Coca (@bcoca)