**1. 简述** ``` template也是个模块, 遵循[jinja2](http://jinja.pocoo.org/ "jinja2")语法, 熟悉python的朋友应该对这个比较了解, 不太了解可以取官网站上看一下下,这里只做简单的介绍 ``` **2. jinja2** ``` 字面量 字符串:使用单引号或双引号; 数字:整数、浮点数; 列表:[item1, item2, ...] 元组:(item1, item2, ...) 字典:{key1:value1, key2:value2, ...} 布尔型 true/false 算术运算: +, -, *, /, //, %, ** 比较操作: ==, !=, >, <, >=, <= 逻辑运算 and, or, not 流表达式 for if when ``` **3. template** ``` 注意: 此模板不能在命令行使用,而只能用于playbook; 示例: --- - hosts: testsrvs remote_user: root vars: http_port: 88 tasks: - name: install package yum: name=nginx - name: copy template template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf notify: restart nginx - name: start service service: name=nginx state=started enabled=yes handlers: - name: restart nginx service: name=nginx state=restarted enabled=yes ``` **4. when** ``` 条件测试: 如果需要根据变量、facts或此前任务的执行结果来作为某task执行与否的前提时要用到 条件测试,在task中使用,jinja2的语法格式 示例: --- - hosts: testsrvs remote_user: root vars: http_port: 88 tasks: - name: install package yum: name=nginx - name: copy template for centos7 template: src=nginx.conf7.j2 dest=/etc/nginx/nginx.conf when: ansible_distribution_major_version == "7" notify: restart nginx - name: copy template for centos6 template: src=nginx.conf6.j2 dest=/etc/nginx/nginx.conf when: ansible_distribution_major_version == "6" notify: restart nginx - name: start service service: name=nginx state=started enabled=yes handlers: - name: restart nginx service: name=nginx state=restarted enabled=yes ``` **5. 迭代** ``` (1) 固定变量名为item {{ item }} (2) tasks中使用with_item给定要迭代的元素 with_items: - file1 - file2 - file3 —————————————— with_items: - { name: 'usert1', group: 'g1'} - { name: 'usert2', group: 'g2'} - { name: 'usert3', group: 'g3'} (3) 列表格式: 字典或字符串 (4) 基于字符串列表给出元素 --------------------------------------------------------------------- --- - hosts: testsrvs remote_user: root tasks: - name: create some file file: name=/data/item }} state=touch when (ansible_distribution_major_version == "7" with_items: - file1 - file2 - file3 - name: install some packages yum: name={{ item) with_items: - htop - sl - hping3 --------------------------------------------------------------------- (5) 基于字典列表给元素示例 --------------------------------------------------------------------- - hosts: all remote_user: root tasks: - name: create group group: name=item }} state=present with_items (- groupx1 - groupx2 - groupx3 - name: create user user: name={{ item.name) group={{ item.group }} state=present with_items: - { name : 'userx1',group : 'groupx1' } - { name : 'userx2',group : 'groupx2' } - { name : 'userx3',group : 'groupx3' } --------------------------------------------------------------------- ``` **6. for/if ** ``` # testfor.yml --- - hosts: all remote_user: root vars: ports: - web1: port: 81 #name: web1.com rootdir: /data/website1 - web2: port: 82 name: web2.com rootdir: /data/website2 - web3: port: 83 name: web3.com rootdir: /data/website3 tasks: - name: cp conf template: src=for1.conf.j2 dest=/data/for1.conf ---------------------------------------------------------- # templates/testfor.conf.j2 {% for p in ports %} server { listen {{ p.port }}; {% if p.name is defined%} server_name {{ p.name }}; {% endif %} root {{ p.rootdir }}; } {% endfor %} ``` 最后修改:2019 年 08 月 21 日 11 : 12 AM © 著作权归作者所有