1. 程式人生 > 其它 >05-jinja2模板

05-jinja2模板

⼀、Ansible Jinja2模板背景介紹

⽬前Nginx的配置⽂件在所有的伺服器上都是相同的,但我希望能根據每⼀臺伺服器的效能去定製服務的啟動程序。

同時定製每⼀臺Nginx服務的響應頭,以便於當某臺服務出現問題時能快速定位到具體的伺服器。

要做這樣的定製勢必會導致⼀個問題,Nginx 在每臺物理伺服器上的配置⽂件都不⼀樣,

這樣的配置⽂件如何管理呢? 再使⽤copy 模組去做管理顯然已經不合適。

此時使⽤Ansible 提供的另⼀個模板(template) 功能,它可以幫助我們完美的解決問題。

⼆、 JinJa2 模板

要學會Ansible 中的模板(template)使⽤,前提我們必須要學會JinJa2模板。學會了它,就相當於我們學會了Ansible 模板。

JinJa2 是什麼

Jinja2是基於Python書寫的模板引擎。功能⽐較類似於PHP的smarty模板。

JinJa2 必知必會

1、jinja2 ⽂件以 .j2 為字尾, 也可以不寫字尾。

2、jinja2 中存在 三種定界符

  • 註釋: {# 註釋內容 #}
  • 變數引⽤: {{ var }}
  • 邏輯表達: {% %}

JinJa2 邏輯控制

條件表達

{% if %}
...
{% elif %}
...
{% else %}
...
{% endif %}

Example

{# 如果定義了 idc 變數, 則輸出 #}
{% if idc is defined %}
{{ idc }}
{% elif %}
 沒有定義
{% endif %}

迴圈控制

{% for %}
...
...
{% endfor %}

Example

{# 列舉出 dbservers 這個 group 中的所有主機 #}
{% for host in groups['dbservers'] %}
{{ host }}
{% endfor %}

{#與Python 語法不通,模板中的迴圈內不能break或continue。但你可以在迭代中過濾序列來跳過某些項#}
{#列印dbservers 組中的所有主機,但是不列印1.1.1.1 這臺主機#}
{% for host in groups['dbservers'] if host !="1.1.1.1" %}
{{host}}
{% endfor %}

三、如何使用模板

⼀個基於Facts的Jinja2 例項

# cat config.j2
{# use variable example #}
{# 都是內建變數 #}
wlecome host {{ ansible_hostname }}, os is {{ ansible_os_family }}
today is {{ ansible_date_time.date }}
cpucore numbers {{ ansible_processor_vcpus }}
{# use condition example #}
{% if ansible_processor_vcpus > 1 %}
OS CPU more than one core
{% endif %}
{% for m in ansible_mounts if m['mount'] != "/" %}
mount {{ m['mount'] }}, total size is
{{m['size_total']}}, free size is
{{m['size_available']}}
{% endfor %}

在Ansible 中使⽤模板

---
- name: a template example
  hosts: all
  remote_user: root
  tasks:
   - name: update jinja2 config
     template: src=config.j2 dest=/tmp/config.conf

四、 例項演示

Jinja2 模板以及如何在Ansible中使⽤模板,已經介紹完了。那麼如何去實現我們的需求呢?

# cat 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;
}

繼續優化我們的PlayBook, 讓它⽀持模板

 vars:
 createuser:
 - tomcat
 - www
 - mysql
 tasks:
 - name: create user
 user: name={{ item }} state=present
 with_items: "{{ createuser }}"
 - name: yum nginx webserver
 yum: name=nginx state=present
 # use ansible template
 - name: update nginx main config
 template:
 src: nginx.conf.j2
 dest: /etc/nginx/nginx.conf
 tags: updateconfig
 notify: reload nginx server
 
 - name: add virtualhost config
 copy:
 src: www.qfedu.com.conf
 dest: /etc/nginx/conf.d/
 tags: updateconfig
 notify: reload nginx server
 
 - name: check nginx syntax
 shell: /usr/sbin/nginx -t
 register: nginxsyntax
 tags: updateconfig
 
 - name: check nginx running
 stat: path=/var/run/nginx.pid
 
  register: nginxrunning
 tags: updateconfig
 
 - name: print nginx syntax
 debug: var=nginxsyntax
 
 - name: start nginx server
 service: name=nginx state=started
 when:
 - nginxsyntax.rc == 0
 - nginxrunning.stat.exists == false
 handlers:
 - name: reload nginx server
 service: name=nginx state=started
 when:
 - nginxsyntax.rc == 0
 - nginxrunning.stat.exists == true

執⾏還是按照原來的⽅式執⾏即可

ansible-playbook -i hosts site.yml

1