7、模板templates
文本文件,嵌套有脚本(使用模板编程语言编写) 借助模板生成真正的文件Jinja2语言,使用字面量,有下面形式 字符串:使用单引号或双引号 数字:整数,浮点数 列表:[item1, item2, ...] 元组:(item1, item2, ...) 字典:{key1:value1, key2:value2, ...} 布尔型:true/false算术运算:+, -, *, /, //, %, **比较操作:==, !=, >, >=, <, <=逻辑运算:and,or,not流表达式:For,If,When
7.1、Jinja2相关
字面量 1> 表达式最简单的形式就是字面量。字面量表示诸如字符串和数值的 Python对象。如“Hello World” 双引号或单引号中间的一切都是字符串。 2> 无论何时你需要在模板中使用一个字符串(比如函数调用、过滤器或只是包含或继承一个模板的参数),如4242.23 3> 数值可以为整数和浮点数。如果有小数点,则为浮点数,否则为整数。在Python 里, 42 和 42.0 是不一样的
算术运算Jinja 允许你用计算值。这在模板中很少用到,但为了完整性允许其存在支持下面的运算符 +:把两个对象加到一起。 通常对象是素质,但是如果两者是字符串或列表,你可以用这 种方式来衔接它们。 无论如何这不是首选的连接字符串的方式!连接字符串见 ~ 运算符。 {{ 1 + 1 }} 等于 2 -:用第一个数减去第二个数。 {{ 3 - 2 }} 等于 1 /:对两个数做除法。返回值会是一个浮点数。 {{ 1 / 2 }} 等于 {{ 0.5 }} //:对两个数做除法,返回整数商。 {{ 20 // 7 }} 等于 2 %:计算整数除法的余数。 {{ 11 % 7 }} 等于 4 *:用右边的数乘左边的操作数。 {{ 2 * 2 }} 会返回 4 。 也可以用于重 复一个字符串多次。{{ ‘=’ * 80 }} 会打印 80 个等号的横条 **:取左操作数的右操作数次幂。 {{ 2**3 }} 会返回 8
比较操作符== 比较两个对象是否相等!= 比较两个对象是否不等> 如果左边大于右边,返回 true>= 如果左边大于等于右边,返回 true< 如果左边小于右边,返回 true<= 如果左边小于等于右边,返回 true逻辑运算符对于 if 语句,在 for 过滤或 if 表达式中,它可以用于联合多个表达式and 如果左操作数和右操作数同为真,返回 trueor 如果左操作数和右操作数有一个为真,返回 truenot 对一个表达式取反(见下)(expr) 表达式组['list', 'of', 'objects']:一对中括号括起来的东西是一个列表。列表用于存储和迭代序列化的数据。例如 你可以容易地在 for循环中用列表和元组创建一个链接的列表 <ul> {% for href, caption in [('index.html', 'Index'), ('about.html', 'About'), ('downloads.html','Downloads')] %} <li><a href="{{ href }}">{{ caption }}</a></li> {% endfor %} </ul> ('tuple', 'of', 'values'):元组与列表类似,只是你不能修改元组。如果元组中只有一个项,你需要以逗号结尾它。元组通常用于表示两个或更多元素的项。更多细节见上面的例子 {'dict': 'of', 'key': 'and', 'value': 'pairs'}:Python 中的字典是一种关联键和值的结构。键必须是唯一的,并且键必须只有一个 值。字典在模板中很少使用,罕用于诸如 xmlattr() 过滤器之类 true / false: true 永远是 true ,而 false 始终是 false
7.2、template 的使用
template功能:根据模块文件动态生成对应的配置文件 > template文件必须存放于templates目录下,且命名为 .j2 结尾 > yaml/yml 文件需和templates目录平级,目录结构如下: ./ ├── temnginx.yml └── templates └── nginx.conf.j2
7.3、template示例
示例:利用template 同步nginx配置文件准备templates/nginx.conf.j2文件vim temnginx.yml- hosts: websrvs remote_user: root tasks: - name: template config to remote hosts template: src=nginx.conf.j2 dest=/etc/nginx/nginx.confansible-playbook temnginx.yml
7.3.1、Playbook中template变更替换
修改文件nginx.conf.j2 下面行为worker_processes {{ ansible_processor_vcpus }};cat temnginx2.yml- hosts: websrvs remote_user: root tasks: - name: template config to remote hosts template: src=nginx.conf.j2 dest=/etc/nginx/nginx.confansible-playbook temnginx2.yml
7.3.2、Playbook中template算术运算
算法运算:示例: vim nginx.conf.j2 worker_processes {{ ansible_processor_vcpus**2 }}; worker_processes {{ ansible_processor_vcpus+2 }};
7.4、template for if when循环
{% for vhost in nginx_vhosts %}server { #重复执行server代码listen {{ vhost.listen | default('80 default_server') }};{% if vhost.server_name is defined %}server_name {{ vhost.server_name }};{% endif %}{% if vhost.root is defined %}root {{ vhost.root }};{% endif %}{% endfor %}
7.4.1、示例
// temnginx.yml---- hosts: testweb remote_user: root vars: # 调用变量 nginx_vhosts: - listen: 8080 #列表 键值对//templates/nginx.conf.j2{% for vhost in nginx_vhosts %} server { listen {{ vhost.listen }}}{% endfor %}生成的结果server { listen 8080}
// temnginx.yml---- hosts: mageduweb remote_user: root vars: nginx_vhosts: - web1 - web2 - web3 tasks: - name: template config template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf// templates/nginx.conf.j2{% for vhost in nginx_vhosts %}server { listen {{ vhost }}}{% endfor %}生成的结果:server { listen web1}server { listen web2}server { listen web3}