自动化运维工具Ansible(10)Jinja2模板

【自动化运维工具Ansible(10)Jinja2模板】知识为进步之母,而进步又为富强之源泉。这篇文章主要讲述自动化运维工具Ansible(10)Jinja2模板相关的知识,希望能为你提供帮助。
一、Ansible Jinja2模板背景介绍目前nginx的配置文件在所有的服务器上都是相同的,但能根据每一台服务器的性能去定制服务的启动进程。同时定制每一台Nginx服务的响应头,以便于当某台服务出现问题时能快速定位到具体的服务器。要做这样的定制势必会导致一个问题,Nginx 在每台物理服务器上的配置文件都不一样,这样的配置文件如何管理呢?再使用copy 模块去做管理显然已经不合适。此时使用Ansible 提供的另一个模板(template) 功能解决问题。


二、JinJa2 模板Jinja2是基于python书写的模板引擎。功能比较类似于php的smarty模板。

  • jinja2 文件以 `.j2` 为后缀, 也可以不写后缀。
  • jinja2 中存在 三种定界符
注释:# 注释内容 #
变量引用:var
逻辑表达:%%

JinJa2 逻辑控制
% if %
...
% elif %
...
% else %
...
% endif %

# 如果定义了 idc 变量, 则输出 #
% if idc is defined %
idc
% elif %
没有定义
% endif %

循环控制
% for %
...
...
% endfor %

# 列举出 dbservers 这个 group 中的所有主机 #
% for host in groups[dbservers] %
host
% endfor %



三、使用模板一个基于Facts的Jinja2
# use variable example #
wlecome hostansible_hostname , os isansible_os_family
today isansible_date_time.date
cpucore numbersansible_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] != "/" %
mountm[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=https://www.songbingjia.com/android/config.j2 dest=/tmp/config.conf
...



四、演示
[root@localhost home]# vim nginx.conf.j2

usernginx;
# start process equal cpu cores #
worker_processesansible_processor_vcpus ;

error_log/var/log/nginx/error.log;
pid/var/run/nginx.pid;

events
worker_connections1024;


http
include/etc/nginx/mime.types;
default_typeapplication/octet-stream;

log_formatmain$remote_addr - $remote_user [$time_local] "$request"
$status $body_bytes_sent "$http_referer"
"$http_user_agent" "$http_x_forwarded_for";

sendfileon;
tcp_nopushon;

keepalive_timeout0;

gzip on;
gzip_min_length1k;
gzip_buffers8 64k;
gzip_http_version 1.0;
gzip_comp_level 5;
gzip_typestext/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_headeransible_hostname ; #
add_header x-hostnameansible_hostname;

include /etc/nginx/conf.d/*.conf;

优化PlayBook, 支持模板
- name: template playbook example
hosts: webservers
vars:
createuser:
- tomcat
- www
- mysql
tasks:
- name: create user
user: name= itemstate=present
with_items: " createuser "

- name: yum nginx webserver
yum: name=nginx state=present

name: yum nginx webserver
yum: name=nginx state=present
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




    推荐阅读