<title>{% block title %}{% endblock %}</title>
<ul>
{% for user in users %}
<li><a href="{{ user.url }}">{{ user.username }}</a></li>
{% endfor %}
</ul>
<!DOCTYPE html>
<html>
<p>Hello {{ username }}</p>
</body>
</html>
We use for loop to iterate over the list_example and print it with this placeholder{{ name }}. To end for loop use {%endfor%} and to end if loop, use {%endif%}.
<!DOCTYPE html>
<html>
<head>
<title>Conditions</title>
</head>
<body>
<ul>
{% for name in list_example %}
<li>{{ name }}</li>
{% endfor %}
</ul>
<ol>
{% for name in list_example %}
<li>{{ name }}</li>
{% endfor %}
</ol>
</body>
</html>
Jinja 2 supports Template Inheritance, which is one of the most powerful and useful features of any template engine. It means one template can inherit from another template.
** /app/templates/base.html **
<! — “Parent Template” →
<!DOCTYPE html>
<html>
<head>
{% block head %}
<title>{% block title %}{% endblock %}</title>
{% endblock %}
</head>
<body>
{% block body %}{% endblock %}
</body>
</html>
** /app/templates/index.html **
<! — “Child Template” →
{% extends “base.html” %}
{% block title %} Index {% endblock %}
{% block head %}
{{ super() }}
{% endblock %}
{% block body %}
<h1>Hello World</h1>
<p>Welcome to my site.</p>
{% endblock %}
/app/templates/404.html
{% extends ‘base.html’ %}
{% block title %} Page Not Found {% endblock %}
{% block body %}
<h1>404 Error :(</h1>
<p>What you were looking for is just not there.<p>
<a href=”{{ url_for(‘index’) }}”>go somewhere nice</a>
{% endblock %}
mysql_conf.yml
---
# MySQL connection settings.
mysql_port: "3306"
mysql_data_dir: "/var/lib/mysql"
mysql_pid_file: "{{ mysql_data_dir }}/mysqld.pid"
mysql_socket: "{{ mysql_data_dir }}/mysql.sock"
# Slow query log settings.
mysql_slow_query_log_enabled: yes
mysql_slow_query_time: "2"
mysql_slow_query_log_file: "{{ mysql_data_dir }}/mysql-slow.log"
# Based on resources
mysql_max_connections: "{{ (ansible_memtotal_mb // 12) | int }}"
# Set .._buffer_pool_size up to 70% of RAM but beware of setting too high.
mysql_innodb_buffer_pool_size: "{{ (ansible_memtotal_mb * 0.7) | int }}M"
# Set .._log_file_size to 25% of buffer pool size.
mysql_innodb_log_file_size: '{{ ((mysql_innodb_buffer_pool_size | string | replace("M", "") | int) * 0.25) | int }}M'
playbook.yml
- hosts: 127.0.0.1
vars_files:
- mysql_conf.yml
tasks:
- name: Creating my.cnf with respected resources
template:
src: mysql_conf.j2
dest: my.cnf
mysql_conf.j2
# {{ ansible_managed }}
[client]
port = {{ mysql_port }}
socket = {{ mysql_socket }}
[mysqld]
port = {{ mysql_port }}
datadir = {{ mysql_data_dir }}
socket = {{ mysql_socket }}
pid-file = {{ mysql_pid_file }}
# Slow query log configuration.
{% if mysql_slow_query_log_enabled %}
slow_query_log = 1
slow_query_log_file = {{ mysql_slow_query_log_file }}
long_query_time = {{ mysql_slow_query_time }}
{% endif %}
# InnoDB settings.
innodb_buffer_pool_size = {{ mysql_innodb_buffer_pool_size }}
innodb_log_file_size = {{ mysql_innodb_log_file_size }}
# Setting max connections
{% if mysql_max_connections | int > 3000 %}
max_connections = 3000
thread_cache_size = {{ (3000 * 0.15) | int }}
{% elif mysql_max_connections | int < 150 %}
max_connections = 150
thread_cache_size = {{ (150 * 0.15) | int }}
{% else %}
max_connections = {{ mysql_max_connections }}
thread_cache_size = {{ (mysql_max_connections | int * 0.15) | int }}
{% endif %}