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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值