Ansible Jinja2模板

一、Ansible Jinja2模板背景介绍

目前Nginx的配置文件在所有的服务器上都是相同的,但我希望能根据每台服务器的性能去定制服务的启动进程。同时定制每台 Nginx 服务的响应头,以便于当某台服务出现问题时能快速定位到具体的服务器。要做这样的定制势必会导致一个问题,Nginx 在每台物理服务器上的配置文件都不一样,这样的配置文件如何管理呢?再使用 copy 模块去做管理显然已经不合适。此时使用 Ansible 提供的另一个模板 (template) 功能,它可以帮助我们完美的解决问题。

二、 JinJa2 模板

要学会Ansible 中的模板(template)使用,前提我们必须要学会 JinJa2模板。学会了它,就相当于我们学会了Ansible 模板。

1、JinJa2 是什么

Jinja2是基于Python书写的模板引擎。功能比较类似于PHP的 smarty模板。

2、JinJa2 必知必会

jinja2 文件以 .j2 为后缀, 也可以不写后缀。
jinja2 中存在三种定界符

  • 注释: {# 注释内容 #}
  • 变量引用: {{ var }}
  • 逻辑表达: {% %}
3、JinJa2 逻辑控制
3.1 条件表达
{% if %}
...
{% elif %}
...
{% else %}
...
{% endif %}

Example

{# 如果定义了 idc 变量, 则输出 #}
{% if idc is defined %}
{{ idc }}
{% elif %}
 没有定义
{% endif %}
3.2 循环控制
{% 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 {
    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/xshockwave-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, 让它支持模板

- name: template playbook example
  hosts: web
  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
  - 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: test.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
    shell: "ps -ef|grep 'nginx: maste[r]'|awk '{print $2}'"
    register: nginxrunning
    tags: updateconfig
  - name: print nginx syntax
    debug:
      var: nginxsyntax
  - name: print nginx syntax
    debug:
      var: nginxrunning
  - name: start nginx server
    service:
      name: nginx
      state: started
    when:
    - nginxsyntax.rc == 0
    - nginxrunning.stdout == ""
    tags: updateconfig
  handlers:
  - name: reload nginx server
    service:
      name: nginx
      state: reloaded
    when:
    - nginxsyntax.rc == 0
    - nginxrunning.stdout != ""

执行还是按照原来的方式执行即可

ansible-playbook -i hosts site.yml
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值