Last active
October 11, 2024 16:44
-
-
Save halberom/794c06598f40ccc31560 to your computer and use it in GitHub Desktop.
ansible - example of template if else
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{# 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"> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
- 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: | |
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Server port={{ tomcat_value }} shutdown="SHUTDOWN"> |
thanks!
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 }
OMG :) Thank you so much
Thanks so much! <3
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Amazing! Thanks for sharing