Skip to content

Instantly share code, notes, and snippets.

@halberom
Last active January 17, 2025 12:34
Show Gist options
  • Save halberom/794c06598f40ccc31560 to your computer and use it in GitHub Desktop.
Save halberom/794c06598f40ccc31560 to your computer and use it in GitHub Desktop.

Revisions

  1. Gerard Lynch revised this gist Nov 7, 2017. 2 changed files with 11 additions and 3 deletions.
    10 changes: 9 additions & 1 deletion opt1_template.j2
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,14 @@
    {% if filepath == /var/opt/tomcat_1 %}
    {# 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">
    4 changes: 2 additions & 2 deletions opt2_play.yml
    Original file line number Diff line number Diff line change
    @@ -6,10 +6,10 @@
    tasks:
    # style 1 using filter
    - set_fact:
    tomcat_value: "{{ (filepath == /var/opt/tomcat_1) | ternary(tomcat_1_value, tomcat_2_value) }}"
    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 }}"
    tomcat_value: "{{ tomcat_1_value if (filepath == '/var/opt/tomcat_1') else tomcat_2_value }}"


    - template:
  2. Gerard Lynch revised this gist Nov 7, 2017. 2 changed files with 9 additions and 4 deletions.
    4 changes: 2 additions & 2 deletions opt1_template.j2
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    {% if filepath == /var/opt/tomcat_1 %}
    {% set tomcat_value == tomcat_1_value %}
    {% set tomcat_value = tomcat_1_value %}
    {% else %}
    {% set tomcat_value == tomcat_2_value %}
    {% set tomcat_value = tomcat_2_value %}
    {% endif %}
    <Server port={{ tomcat_value }} shutdown="SHUTDOWN">
    9 changes: 7 additions & 2 deletions opt2_play.yml
    Original file line number Diff line number Diff line change
    @@ -4,8 +4,13 @@
    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) }}"

    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:
    ...
  3. Gerard Lynch revised this gist Jul 29, 2015. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions opt2_play.yml
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,8 @@
    ---
    - hosts: foo
    vars:
    tomcat_1_value: 'bob'
    tomcat_2_value: 'bar'
    tasks:
    - set_fact:
    tomcat_value: "{{ filepath == /var/opt/tomcat_1 | ternary(tomcat_1_value, tomcat_2_value) }}"
  4. Gerard Lynch created this gist Jul 29, 2015.
    6 changes: 6 additions & 0 deletions opt1_template.j2
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    {% if filepath == /var/opt/tomcat_1 %}
    {% set tomcat_value == tomcat_1_value %}
    {% else %}
    {% set tomcat_value == tomcat_2_value %}
    {% endif %}
    <Server port={{ tomcat_value }} shutdown="SHUTDOWN">
    8 changes: 8 additions & 0 deletions opt2_play.yml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    ---
    - hosts: foo
    tasks:
    - set_fact:
    tomcat_value: "{{ filepath == /var/opt/tomcat_1 | ternary(tomcat_1_value, tomcat_2_value) }}"

    - template:
    ...
    1 change: 1 addition & 0 deletions opt2_template.j2
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    <Server port={{ tomcat_value }} shutdown="SHUTDOWN">