j2 template
In some cases, we can hope to generate files with different content for different hosts, and we can use the j2 template to achieve this function.
Use of J2 template
- tasks:
template:
src: my.cnf.j2
dest: /tec/my.cnf
J2 template syntax
Comment
{# context #}
variable
{{ var_name }}
Conditional judgment
{% if mysql_version == 5.6 %}
read_only=1
{% else %}
read_only=1
super_read_only=1
{% endif %}
cycle
Simple loop
{% for item in list %}
{{ item }}
{% endfor %}
Cycle and condition judgment
{% for item in list if not myuser == "root" %}
User number {{ loop.index }} - {{ myuser }}
{% endfor %}
More syntax can be viewed: Template Designer Documentation