Ansible(五)j2模版

j2模版

在某些情況下,我們可以希望針對不同的主機生成不同內容的文件,可以使用j2模版實現這種功能。

J2模版的使用

- tasks:
  template:
     src: my.cnf.j2
     dest: /tec/my.cnf

J2模版語法

註釋

{# context #}

變量

{{ var_name }}

條件判斷

{% if mysql_version == 5.6 %}
read_only=1
{% else %}
read_only=1
super_read_only=1
{% endif %}

循環

簡單循環

{% for item in list %}
    {{ item }}
{% endfor %}

循環和條件判斷

{% for item in list if not myuser == "root" %}
    User number {{ loop.index }} - {{ myuser }}
{% endfor %}

更多語法可以查看:Template Designer Documentation

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章