Jinja2 簡介
Jinja2 是基于 Python 的模闆引擎,功能比較類似于 PHP 的 smarty。
在 Ansible 中通過 template 子產品使用 JinJa2 模闆。
·
Jinja2 文法
jinja2 檔案以 .j2 為字尾, 也可以不寫字尾。
jinja2 中存在三種定界符
- 注釋: {# 注釋内容 #}
- 變量引用: {{ var }}
- 邏輯表達: {% %}
·
Jinja2 邏輯控制
·
條件判斷邏輯控制
{% if %}
...
{% elif %}
...
{% else %}
...
{% endif %}
示例:
cat << EOF > ifjinjia.j2
{# 如果定義了 idc 變量, 則輸出 #}
{% if idc is defined %}
{{ idc }}
{% else %}
idc is not defined
{% endif %}
EOF
·
循環邏輯控制
{% for %}
...
...
{% endfor %}
示例:
cat << EOF > forjinjia.j2
{# 列舉出 ansible 這個 group 中的所有主機 #}
{% for host in groups['ansible'] %}
{{ host }}
{% endfor %}
·
使用 Jinjia2 模闆
示例一:一個基于 Facts 變量的 Jinja2 模闆
cat << EOF > factsjinjia.j2
{# use variable example #}
my hostname is {{ ansible_hostname }}, os is {{ ansible_os_family }}
today is {{ ansible_date_time.date }}
cpucore numbers {{ ansible_processor_vcpus }}
{# use if condition example #}
{% if ansible_processor_vcpus > 1 %}
OS CPU more than one core
{% endif %}
{# use for loop example #}
{% for m in ansible_mounts %}
{{ m['mount'] }}
{% endfor %}
{# use for loop and if condition example #}
{% for m in ansible_mounts if m['mount'] != "/" %}
mount {{ m['mount'] }}, total size is {{m['size_total']}}, free size is {{m['size_available']}}
{% endfor %}
EOF
在Ansible 中使用 factsjinjia.j2 模闆
---
- name: a template example
hosts: all
remote_user: root
tasks:
- name: update jinja2 config
template: src=factsjinjia.j2 dest=/tmp/factsjinjia.conf
[[email protected] ~]# cat /tmp/factsjinjia.conf
my hostname is wpf002, os is RedHat
today is 2021-02-22
cpucore numbers 2
OS CPU more than one core
/boot
/
mount /boot, total size is 1063256064, free size is 906735616
·
示例二:在 nginx.conf 中使用 jinjia2 模闆
cat << EOF > nginx.conf.j2
user nginx;
{# start process equal cpu cores #}
worker_processes {{ ansible_processor_vcpus }};
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
sendfile on;
tcp_nopush on;
keepalive_timeout 0;
gzip on;
gzip_min_length 1k;
gzip_buffers 8 64k;
gzip_http_version 1.0;
gzip_comp_level 5;
gzip_types text/plain application/x-javascript text/css application/json application/xml application/x-shockwave-flash application/javascript image/svg+xml image/x-icon;
gzip_vary on;
{# add_header {{ ansible_hostname }}; #}
add_header x-hostname {{ ansible_hostname }};
include /etc/nginx/conf.d/*.conf;
}
EOF
---
- name: task control playbook example
hosts: wpf002
tasks:
- name: yum install nginx
yum: name=nginx state=installed
- name: update nginx config
template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf backup=yes
tags: updateconfig
notify: reload nginx server
- name: add nginx virtualhost config
copy: src=wpf.com.conf dest=/etc/nginx/conf.d/
tags: updateconfig
notify: reload nginx server
- name: check nginx running
stat: path=/var/run/nginx.pid
register: nginxrunning
tags: updateconfig
- name: check nginx syntax
shell: /usr/sbin/nginx -t
register: nginxsyntax
tags: updateconfig
- name: start nginx
systemd: name=nginx state=started
when: nginxsyntax.rc == 0 and nginxrunning.stat.exists == false
tags: updateconfig
handlers:
- name: reload nginx server
systemd: name=nginx state=reloaded
when: nginxsyntax.rc == 0 and nginxrunning.stat.exists == true
tags: updateconfig