一、循环
循环迭代任务
1.简单循环
loop: ##赋值列表- value1- value2- ...
{{item}} ##迭代变量名称#实例#
---
- name: create filehosts: 172.25.0.254tasks:- name: file modulefile:name: /mnt/{{item}}state: presentloop:- westos_file1- westos_file2
2.循环散列或字典列表
---
- name: create filehosts: 172.25.0.254tasks:- name: file moduleservice:name: "{{ item.name}}"state: "{{ item.state }}"loop:- name: httpdstate: started- name: vsftpdstate: stopped
二、条件
when:- 条件1- 条件2
#条件判断#
= value == "字符串",value == 数字
< value < 数字
> value > 数字
<= value <= 数字
>= value >= 数字
!= value != 数字
is defined value value is defined 变量存在
is not defined value is not defined 变量不存在
in value is in value 变量为
not in value is not in value 变量不为
bool变量 为true value value的值为true
bool变量 false not value value的值为falsevalue in value2 value的值在value2列表中
#多条条件组合#
when:条件1 and 条件2- 条件1- 条件2
when:条件1 or 条件2
when: >条件1or条件2
true和false
三、触发器
notify: 触发器当遇到更改是触发handlers
handlers: 触发器触发后执行的动作
#实例#
---
- name: create virtualhost for web serverhosts: 172.25.0.254vars_files:./vhost_list.ymltasks:- name: create documentfile:path: "{{web2.document}}"state: directory- name: create vhost.confcopy:dest: /etc/httpd/conf.d/vhost.confcontent:"<VirtualHost *:{{web1.port}}>\n\tServerName {{web1.name}}\n\tDocumentRoot
{{web1.document}}\n\tCustomLog logs/{{web1.name}}.log combined\n</VirtualHost>\n\n<VirtualHost *:
{{web2.port}}>\n\tServerName {{web2.name}}\n\tDocumentRoot {{web2.document}}\n\tCustomLog logs/
{{web2.name}}.log combined\n</VirtualHost>"notify:restart apachehandlers:- name: restart apacheservice:name: httpdstate: restarted
四、处理失败任务
1.ignore_errors
#作用:
当play遇到任务失败时会终止
ignore_errors: yes 将会忽略任务失败使下面的任务继续运行
#实例#
- name: check file playhosts: alltasks:- name: check fileshell:test -e /mnt/fileignore_errors: yesregister: check_state- name: show messagedebug:msg: hello world
2.force_handlers
#作用:
#当任务失败后play被终止也会调用触发器进程
#example
---
- name: apache change porthosts: 172.25.0.254force_handlers: yesvars:http_port: 80tasks:- name: configure apache conf filelineinfile:path: /etc/httpd/conf/httpd.confregexp: "^Listen"line: "Listen {{ http_port }}"notify: restart apache- name: install errordnf:name: westosstate: latesthandlers:- name: restart apacheservice:name: httpdstate: restartedenabled: yes
3.changed_when
#作用:
#控制任务在何时报告它已进行更改;强制更改:true,强制不更改:false
---
- name: apache change porthosts: 172.25.0.254force_handlers: yesvars:http_port: 8080tasks:- name: configure apache conf filelineinfile:path: /etc/httpd/conf/httpd.confregexp: "^Listen"line: "Listen {{ http_port }}"changed_when: truenotify: restart apachehandlers:- name: restart apacheservice:name: httpdstate: restartedenabled: yes
4.failed_when
#当符合条件时强制任务失败
#强制某个play为失败状态
---
- name: testhosts: 172.25.0.254tasks:- name: shellshell: echo helloregister: westosfailed_when: "'hello' in westos.stdout"
5.block
block: ##定义要运行的任务
rescue: ##定义当block句子中出现失败任务后运行的任务##block运行成功,则rescue不运行
always: ##定义最终独立运行的任务
五、 练习
建立playbook ~/westos.yml要求如下:
建立大小为1500M名为/dev/vdb1的设备
如果/dev/vdb不存在请输入:/dev/vdb is not exist
如果/dev/vdb大小不足1.5G请输出:/dev/vdb is less then 1.5G
并建立800M大小的/dev/vdb1
此设备挂载到/westos上
- name: test fdiskhosts: alltasks:- name: check sdbdebug:msg: /dev/sdb is not existwhen: ansible_facts['devices']['sdb'] is not defined- name: create /dev/sdb1block:- name: check sizeparted:device: /dev/sdbnumber: 1state: presentpart_end: 1.5GiBwhen: ansible_facts['devices']['sdb'] is defined- name: show size is not enough 1.5Gdebug:msg: /dev/sdb is not enough 1.5Grescue:- name: create /dev/sdb1parted:device: /dev/sdbnumber: 1state: presentpart_end: 800MiBnotify:- set filesystem- mount /dev/sdb1when: ansible_facts['devices']['sdb'] is definedalways:- name: create mount pointfile:path: /westosstate: directoryhandlers:- name: set filesystemfilesystem:fstype: ext4dev: /dev/sdb1- name: mount /dev/sdb1mount:path: /westos/src: /dev/sdb1fstype: ext4state: mounted