Ansibel中的Playbook
1. 理解
相当于一种剧本,其中有多条要执行的命令。
2. 变量
全局变量:在命令后面-e添加变量为全局变量。
资产变量:在资产中生命的变量
剧本变量:在playbook中声明的变量
系统中的facts变量
优先级:
全局变量 > 资产变量> 剧本变量>facts变量
3. 控制循环
with_item:控制变量循环
item:固定变量名称,不可变
4. 判断,控制变量
在命令后用 关键字 when 变量名==参数值 来控制判断条件。
5. tags 标签
命令后加-t 用来标志,执行时只执行在playbook中用tags标记过的命令
6. handlers
handler与notify共同使用:例如:
- name: test.yml just for test
hosts: testserver
vars:
region: ap-southeast-1
tasks:
- name: template configuration
file template: src=template.j2 dest=/etc/foo.conf
notify:
- restart memcached
- restart apache
handlers:
- name: restart memcached
service: name=memcached state=restarted
- name: restart apache
service: name=apache state=restarted