Skip to content

Instantly share code, notes, and snippets.

@wolftales
Created March 31, 2023 16:05
Show Gist options
  • Save wolftales/bc46a4954fd3aa2448985c1c78d679f2 to your computer and use it in GitHub Desktop.
Save wolftales/bc46a4954fd3aa2448985c1c78d679f2 to your computer and use it in GitHub Desktop.
Ansible variable conditional behavior example: if-elif-else
Nested if-else statements that replicate the elif capability:
- name: set_fact based on varible
set_fact:
colour_fruit: "{{ 'green' if fruit == 'apples' else 'yellow' if fruit == 'bananas' else 'unknown' }}"
(A if True else B if True else C)
Can be nested further for additional elif's at the expense of readability
Source
HOWTO: Use Ansible to set replicate a if-elif-else variable
https://linuxbuff.wordpress.com/2020/05/06/howto-use-ansible-to-set-replicate-a-if-then-else-variable/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment