Last active
January 17, 2025 12:34
-
-
Save halberom/794c06598f40ccc31560 to your computer and use it in GitHub Desktop.
ansible - example of template if else
This file contains hidden or 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 hidden or 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 hidden or 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"> |
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
Can the following logic be converted into the template using variables?
where
gi_patches
is an array: