⾃动化运维利器 Ansible-Jinja2

Ansible-Jinja2

  • [一、Ansible Jinja2模板背景介绍](#一、Ansible Jinja2模板背景介绍)
  • [二、 JinJa2 模板](#二、 JinJa2 模板)
    • [2.1 JinJa2 是什么](#2.1 JinJa2 是什么)
    • [2.2 JinJa2逻辑控制](#2.2 JinJa2逻辑控制)
  • 三、如何使用模板
  • 四、实例演示

按顺序食用,口味更佳

( 1 ) ⾃动化运维利器Ansible-基础

( 2 ) ⾃动化运维利器 Ansible-Playbook

( 3 ) ⾃动化运维利器 Ansible-变量

( 4 ) ⾃动化运维利器AnsiblePlaybook的任务控制

( 5 ) ⾃动化运维利器 Ansible-Jinja2

( 6 ) ⾃动化运维利器 Ansible-最佳实战

一、Ansible Jinja2模板背景介绍

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

二、 JinJa2 模板

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

2.1 JinJa2 是什么

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

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

2.2 JinJa2逻辑控制

条件表达

shell 复制代码
{% if %}
...
{% elif %}
...
{% else %}
...
{% endif %}

Example

shell 复制代码
{# 如果定义了 idc 变量, 则输出 #}
{% if idc is defined %}
{{ idc }}
{% elif %}
 没有定义
{% endif %}

循环控制

powershell 复制代码
{% for %}
...
...
{% endfor %}

Example

powershell 复制代码
{# 列举出 dbservers 这个 group 中的所有主机 #}
{% for host in groups['dbservers'] %}
{{ host }}
{% endfor %}
powershell 复制代码
{#与Python 语法不通,模板中的循环内不能break或continue#}
{#你可以在迭代中过滤序列来跳过某些项#}
{#打印dbservers 组中的所有主机,但是不打印1.1.1.1 这台主机#}
{% for host in groups['dbservers'] if host !="1.1.1.1" %}
{{host}}
{% endfor %}

三、如何使用模板

⼀个基于Facts的Jinja2 实例

shell 复制代码
# 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 中使⽤模板

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

四、实例演示

Jinja2 模板以及如何在Ansible中使⽤模板,已经介绍完了。那么如何去实现我们的需求呢?

nginx.conf.j2

bash 复制代码
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"
	.... // 此处配置就省略了...
	
 {# add_header {{ ansible_hostname }}; #}
 add_header x-hostname {{ ansible_hostname }};
 include /etc/nginx/conf.d/*.conf;
}

上述是一个nginx的配置文件,使用JinJa2模板后,ansible就能够通过{{arg}}使用对应节点的变量,而不是写死的数据。

改进后的Playbook:

yaml 复制代码
- name: template playbook example
  hosts: webservers
  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

执⾏还是按照原来的⽅式执⾏即可

使用JinJa2模板就不可以不必每个资产节点都准备一份配置文件,ansible能够使用template注入变量,为节点定制不同的配置。

按顺序食用,口味更佳

( 1 ) ⾃动化运维利器Ansible-基础

( 2 ) ⾃动化运维利器 Ansible-Playbook

( 3 ) ⾃动化运维利器 Ansible-变量

( 4 ) ⾃动化运维利器AnsiblePlaybook的任务控制

( 5 ) ⾃动化运维利器 Ansible-Jinja2

( 6 ) ⾃动化运维利器 Ansible-最佳实战

相关推荐
乘云数字DATABUFF1 天前
5分钟部署开源APM Databuff:OpenTelemetry全链路追踪入门实战
运维·后端
悠然南风3 天前
Ansible常见模块总结及LDAP Role 编写与调试
ansible
荣--3 天前
一键部署不是为了省时间 —— 它是把"买来的 PaaS"变成"自己的平台"的拐点
运维·zabbix·工程化·一键部署·平台化·边界设计
江华森3 天前
动手实战学 Docker — 从零到集群编排完全指南
运维
Avan_菜菜4 天前
FRP 内网穿透完整实战:从 HTTP 映射到 HTTPS 自签代理
运维·nginx·https
SelectDB5 天前
Litefuse 开源并推出单进程轻量模式,25 秒就能跑起来的 Agent 可观测与评估平台
运维·后端·自动化运维
XIAOHEZIcode6 天前
Linux系统鼠标偏移常见原因以及修复方案
linux·运维·游戏
用户0328472220707 天前
如何搭建本地yum源(上)
运维
大树8810 天前
金刚石散热越强,管路越先见顶
大数据·运维·服务器·人工智能·ai
摇滚侠10 天前
Linux CentOS7 rpm 安装 MySQL 5.7
linux·运维·mysql