Ansible 系列之 Jinja2 模板

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
[root@wpf002 ~]# 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

你可能感兴趣的:(Ansible)