模板templates
文本文件,嵌套有脚本(使用模板编程语言编写)
Jinja2语言,使用字面量,有下面形式
算术运算: +, -, *, /, //, %, **
比较操作: ==, !=, >, >=, <, <=
逻辑运算: and, or, not
流表达式: For If When
emplates功能:根据模块文件动态生成对应的配置文件
./
├── temnginx.yml
└── templates
└── nginx.conf.j2
Templates示例
示例:利用templates 同步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.conf
ansible-playbook temnginx.yml
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.conf
ansible-playbook temnginx2.yml
条件测试:如果需要根据变量、 facts或此前任务的执行结果来做为某task执行与否的前提时要用到条件测试,通过when语句实现,在task中使用, jinja2的语法格式
when语句
在task后添加when子句即可使用条件测试; when语句支持Jinja2表达式语法
示例:
tasks:
- name: "shutdown RedHat flavored systems"
command: /sbin/shutdown -h now
when: ansible_os_family == "RedHat"
示例:分别向两台目标主机复制httpd配置文件(一台Centos6,另一台Centos7),在这里使用when进行条件判断,如果为Centos6则复制http_6.conf.j2,如果为Centos7,则复制http_7.conf.j2
- hosts: appsrvs
remote_user: root
tasks:
- name: install package
yum: name=httpd
- name: template 6
template: src=httpd_6.conf.j2 dest=/etc/httpd/conf/httpd.conf
notify: restart service
when: ansible_distribution_major_version == "6"
- name: template 7
template: src=httpd_7.conf.j2 dest=/etc/httpd/conf/httpd.conf
notify: restart service
when: ansible_distribution_major_version == "7"
- name: start service
service: name=httpd state=started
handlers:
- name: restart service
service: name=httpd state=restarted
迭代:当有需要重复性执行的任务时,可以使用迭代机制
对迭代项的引用,固定变量名为” item“
要在task中使用with_items给定要迭代的元素列表
列表格式:
字符串
字典
示例:在目标主机上分别复制空文件f1,f2,f3
---
- hosts: appsrvs
remote_user: root
tasks:
- name: config file
copy: src=/data/{{item}} dest=/data/
with_items:
- file1
- file2
- file3
---
- hosts: appsrvs
remote_user: root
tasks:
- name: create user
user: name={{item}}
with_items:
- xixi
- haha
- memeda
示例:在目标主分别创建三个组group1、group2、group3和三个用户user1、user2、user3,并且使用户和组一一对应
- hosts: websrvs
remote_user: root
tasks:
- name: add some groups
group: name={{ item }} state=present
with_items:
- group1
- group2
- group3
- name: add some users
user: name={{ item.name }} group={{ item.group }} state=present
with_items:
- { name: 'user1', group: 'group1' }
- { name: 'user2', group: 'group2' }
- { name: 'user3', group: 'group3' }
for
示例:在目标主机动态生成for.conf文件
#vim for.yml
- hosts: appsrvs
remote_user: root
vars:
ports:
- 81
- 82
▽ - 83
tasks:
- name: test for
template: src=for.conf.j2 dest=/data/for.conf
#vim for.conf.j2
{% for p in ports %}
server {
listen {{p}}
}
{%endfor%}
示例二:在目标主机上动态生成for1.conf文件
vim for1.yml
- hosts: appsrvs
remote_user: root
vars:
ports:
- web1:
listen: 81
name: www.magedu.com
root: /data/web1
- web2:
▽ listen: 82
name: mobi.magedu.com
root: /data/web2
- web3:
listen: 83
name: app.magedu.com
root: /data/web3
tasks:
- name: test for
template: src=for1.conf.j2 dest=/data/for1.conf
#vim for1.conf.j2
{% for p in ports %}
server {
listen {{p.listen}}
servername {{p.name}}
rootdir {{p.root}}
}
{%endfor%}
if
{% if vhost.server_name is defined %}
server_name {{ vhost.server_name }};
{% endif %}