Skip to content

Instantly share code, notes, and snippets.

@halberom
Last active April 12, 2024 11:33
Show Gist options
  • Star 81 You must be signed in to star a gist
  • Fork 27 You must be signed in to fork a gist
  • Save halberom/794c06598f40ccc31560 to your computer and use it in GitHub Desktop.
Save halberom/794c06598f40ccc31560 to your computer and use it in GitHub Desktop.
ansible - example of template if else
{# style 1 - long form #}
{% if filepath == '/var/opt/tomcat_1' %}
{% set tomcat_value = tomcat_1_value %}
{% else %}
{% set tomcat_value = tomcat_2_value %}
{% endif %}
{# style 2 - short form #}
{% set tomcat_value = tomcat_1_value if (filepath == '/var/opt/tomcat_1') else tomcat_2_value %}
{# style 3 - with ternary filter #}
{% set tomcat_value = (filepath == '/var/opt/tomcat_1')|ternary(tomcat_1_value, tomcat_2_value) %}
<Server port={{ tomcat_value }} shutdown="SHUTDOWN">
---
- hosts: foo
vars:
tomcat_1_value: 'bob'
tomcat_2_value: 'bar'
tasks:
# style 1 using filter
- set_fact:
tomcat_value: "{{ (filepath == '/var/opt/tomcat_1') | ternary(tomcat_1_value, tomcat_2_value) }}"
# style 2
- set_fact:
tomcat_value: "{{ tomcat_1_value if (filepath == '/var/opt/tomcat_1') else tomcat_2_value }}"
- template:
...
<Server port={{ tomcat_value }} shutdown="SHUTDOWN">
@rimiti
Copy link

rimiti commented Jun 14, 2017

Thanks for sharing ;)

@Dmitri10
Copy link

Indeed! Thank you

@racingferret
Copy link

FYI, the set statement only needs one "=", not "==". Thanks for sharing!

@XDanny322
Copy link

Good stuff - Thank you sir!

@bovito
Copy link

bovito commented Jul 17, 2018

Thank you!

@Marwan-Gh
Copy link

thanks for sharing

@sagespidy
Copy link

Thanks . you are awesome

@vi2co
Copy link

vi2co commented Mar 21, 2020

Thanks!

@daretogo
Copy link

👍 Thanks again!

@Umptor
Copy link

Umptor commented Dec 17, 2020

Amazing! Thanks for sharing

@izghitu
Copy link

izghitu commented Jan 29, 2021

thanks!

@jcnars
Copy link

jcnars commented Jul 13, 2021

Can the following logic be converted into the template using variables?

- name: Patch | Get the highest version number from gi_patches array
  set_fact:
    # Using expression from bms-toolkit/roles/rac-db-setup/tasks/rac-db-install.yml Line 46
    highest_version_num: "{% if not ansible_loop.first %}{% if item is version(highest_version_num, '>=') %}{{ item }}{% else %}{{ highest_version_num }}{% endif %}{% else %}{{ item }}{% endif %}"
  loop: "{{ gi_patches | json_query('[?base==`' + oracle_ver + '`].release') }}"
  loop_control:
    extended: yes
  when:
    - oracle_rel is defined
    - oracle_rel != "base"

where gi_patches is an array:

  - { category: "PSU", base: "11.2.0.4.0", release: "11.2.0.4.190416", patchnum: "29255947", patchfile: "p29255947_112040_Linux-x86-64.zip", patch_subdir: "", prereq_check: FALSE, method: "opatch auto", ocm: TRUE, upgrade: FALSE }
  - { category: "PSU", base: "11.2.0.4.0", release: "11.2.0.4.190716", patchnum: "29698727", patchfile: "p29698727_112040_Linux-x86-64.zip", patch_subdir: "", prereq_check: FALSE, method: "opatch auto", ocm: TRUE, upgrade: FALSE }

@SinSolis
Copy link

OMG :) Thank you so much

@tranvanthuc
Copy link

Thanks so much! <3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment