Jinja2 templates
Revision as of 16:47, 11 March 2022 by Pio2pio (talk | contribs) (Pio2pio moved page Jinja templates to Jinja2 templates without leaving a redirect)
The example below show use of Jinja2 to generate Bash script that reads dynamic data from EC2 AWS Metadata. The solution helps setting up JVM params based on instance type as these come with different memory sizes.
################################################################################################### # Ansible group file: server-nodes ################################################################################################### jvm_memory : - tier : 1 list : " m1.small | m1.medium | m1.large | m1.xlarge | t1.micro " xms : 256m xmx : 1024m maxpermsize : 512m - tier : 2 list : " t2.medium | c3.large | c5.large " xms : 2048m xmx : 2048m maxpermsize : 1024m - tier : 3 list : " t2.large | t3.large | c5.xlarge " xms : 2048m xmx : 4096m maxpermsize : 1024m - tier : 4 list : " t2.xlarge | t3.xlarge | c5d.large | c4.2xlarge | c5.2xlarge " xms : 2048m xmx : 8192m maxpermsize : 2048m ################################################################################################### jvm_memory_proxy : '{{ jvm_memory }}' jvm_memory_lb : '{{ jvm_memory }}'
################################################################################################### # Bash template: server_start.sh.j2 ################################################################################################### #!/bin/sh INSTANCE_TYPE=`wget -q -O - http://169.254.169.254/latest/meta-data/instance-type` case $INSTANCE_TYPE in {% if inventory_hostname in groups['server-nodes'] | default([]) %} {% for proxy in jvm_memory_proxy %} {{proxy.list}} ) XMS={{proxy.xms }} XMX={{proxy.xmx }} MAXPERMSIZE={{ proxy.maxpermsize }} ;; {% endfor %} {% elif 'proxy-api-nodes' in group_names and inventory_hostname in groups['proxy-api-nodes'] | default([]) %} {% for proxy in jvm_memory_proxy_api %} {{proxy.list}} ) XMS={{proxy.xms }} XMX={{proxy.xmx }} MAXPERMSIZE={{ proxy.maxpermsize }} ;; {% endfor %} {% elif inventory_hostname in groups['proxy-wkr-nodes'] | default([]) %} {% for proxy in jvm_memory_proxy_wkr %} {{proxy.list}} ) XMS={{proxy.xms }} XMX={{proxy.xmx }} MAXPERMSIZE={{ proxy.maxpermsize }} ;; {% endfor %} {% endif %} * ) XMS={{ jvm_memory_xms }} XMX={{ jvm_memory_xmx }} MAXPERMSIZE={{ jvm_memory_maxpermsize }} ;; esac ###################################################################################################