##一.循环##
#循环迭代任务#
#1.简单循环#
loop:
- value1
- value2
- ... ##赋值列表
{{item}} ##迭代变量名称
#实例#
---
- name: create file
hosts: 172.25.0.254
tasks:
- name: file module
file:
name: /mnt/{{item}}
state: present
loop:
- westos_file1
- westos_file2
#2.循环散列或字典列表#
---
- name: create file
hosts: 172.25.0.254
tasks:
- name: file module
service:
name: "{{ item.name}}"
state: "{{ item.state }}"
loop:
- name: httpd
state: started
- name: vsftpd
state: stopped
实例:
查询受控主机的硬盘信息
[devops@westoslinux .ansible]$ vim device.yml
- name: test
hosts: westos
tasks:
- name: check device
lineinfile:
path: /mnt/device
line: "{{item.name}}={{item.size}}"
create: yes
loop:
- name: vda
size: "{{ ansible_facts['devices']['size']| default('none') }}"
- name: vdb
size: "{{ ansible_facts['devices']['size']| default('none') }}"
[devops@westoslinux .ansible]$ ansible-playbook device.yml
PLAY [test] ***************************************************************************
TASK [Gathering Facts] ****************************************************************
ok: [172.25.254.198]
TASK [check device] *******************************************************************
changed: [172.25.254.198] => (item={'name': 'vda', 'size': 'none'})
changed: [172.25.254.198] => (item={'name': 'vdb', 'size': 'none'})
PLAY RECAP ****************************************************************************
172.25.254.198 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@qwe ~]# cat /mnt/device
vda=none
vdb=none
test:
[devops@westoslinux .ansible]$ vim device1.yml
tasks:
- name: check device
get_url:
url: http://172.25.254.250/RHCE_EXAM/files/hwreport.empty
dest: /mnt/hwreport.txt
force: yes
- name: check device
lineinfile:
path: /mnt/hwreport.txt
regexp: "{{item.name}}"
line: "{{item.name}}={{item.size}}"
create: yes
loop:
- name: HOST
size: "{{ansible_facts['fqdn']}}"
- name: MEMERY
size: "{{ansible_memtotal_mb}}MB"
- name: BIOS
size: "{{ansible_facts['bios_version']}}"
- name: vda
size: "{{ansible_facts['device']['vda']['size']| default('NONE')}}"
- name: vdb
size: "{{ansible_facts['device']['vdb']['size']| default('NONE')}}"
[devops@westoslinux .ansible]$ ansible-playbook device1.yml
##二.条件##
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的值为false value in value2 value的值在value2列表中 |
已知只有198主机/mnt/有test文件 213主机没有
[devops@westoslinux .ansible]$ vim test1.yml
- name: test
hosts: all
tasks:
- name: check file
shell: test -e /mnt/test
register: CHECKOUT
ignore_errors: yes
- name: debug
debug:
msg: "/mnt/test is not exist"
when:
CHECKOUT.rc != 0
- name: debug exist
debug:
msg: "/mnt/test is exist"
when:
CHECKOUT.rc == 0
[devops@westoslinux .ansible]$ ansible-playbook test1.yml
PLAY [test] *************************************************************************
TASK [Gathering Facts] **************************************************************
fatal: [172.25.254.213]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: devops@172.25.254.213: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).", "unreachable": true}
ok: [172.25.254.198]
TASK [check file] *******************************************************************
changed: [172.25.254.198]
TASK [debug] ************************************************************************
skipping: [172.25.254.198]
TASK [debug exist] ******************************************************************
ok: [172.25.254.198] => {
"msg": "/mnt/test is exist"
}
PLAY RECAP **************************************************************************
172.25.254.198 : ok=3 changed=1 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
172.25.254.213 : ok=0 changed=0 unreachable=1 failed=0 skipped=0 rescued=0 ignored=0
已知只有198主机/mnt/有test文件 213主机没有
[devops@westoslinux .ansible]$ vim test3.yml
- name: test
hosts: all
tasks:
- name: check test
shell: test -e /mnt/file1
register: out
ignore_errors: yes
- name: debug
debug:
msg: "/mnt/test is not exist {{out.rc}}"
when:
- out.rc 不存在为1
- name: debug
debug:
msg: "/mnt/test is exist {{out.rc}}"
when:
- not out.rc 存在为0
[devops@westoslinux .ansible]$ ansible-playbook test3.yml
PLAY [test] *************************************************************************
TASK [Gathering Facts] **************************************************************
fatal: [172.25.254.213]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: devops@172.25.254.213: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).", "unreachable": true}
ok: [172.25.254.198]
TASK [check test] *******************************************************************
changed: [172.25.254.198]
TASK [debug] ************************************************************************
skipping: [172.25.254.198]
TASK [debug] ************************************************************************
ok: [172.25.254.198] => {
"msg": "/mnt/test is exist 0"
}
PLAY RECAP **************************************************************************
172.25.254.198 : ok=3 changed=1 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
172.25.254.213 : ok=0 changed=0 unreachable=1 failed=0 skipped=0 rescued=0 ignored=0
#多条条件组合#
条件1与条件2并且关系,共同成立才实施。
when:
条件1 and 条件2
- 条件1
- 条件2
已知198和213主机都有/mnt/test文件
[devops@westoslinux .ansible]$ vim test2.yml
- name: test
hosts: all
tasks:
- name: check file
shell: test -e /mnt/test
register: CHECKOUT
ignore_errors: yes
when: inventory_hostname in groups.westos
- name: debug
debug:
msg: "/mnt/test is not exist"
when:
- inventory_hostname in groups.westos
- CHECKOUT.rc != 0
- name: debug exist
debug:
msg: "/mnt/test is exist"
when:
- inventory_hostname in groups.westos
- CHECKOUT.rc == 0
[devops@westoslinux .ansible]$ ansible-playbook test2.yml
PLAY [test] *************************************************************************
TASK [Gathering Facts] **************************************************************
fatal: [172.25.254.213]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: devops@172.25.254.213: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).", "unreachable": true}
ok: [172.25.254.198]
TASK [check file] *******************************************************************
changed: [172.25.254.198]
TASK [debug] ************************************************************************
skipping: [172.25.254.198]
TASK [debug exist] ******************************************************************
ok: [172.25.254.198] => {
"msg": "/mnt/test is exist"
}
PLAY RECAP **************************************************************************
172.25.254.198 : ok=3 changed=1 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
172.25.254.213 : ok=0 changed=0 unreachable=1 failed=0 skipped=0 rescued=0 ignored=0
条件1与条件2或者关系,一条成立立即实施。
when:
条件1 or 条件2
when: >
条件1
or
条件2
##三.触发器##
notify: 触发器当遇到更改是触发handlers
handlers: 触发器触发后执行的动作
实例:
vim apache1.yml
重新执行此剧本会发现没有更改剧本里的任何东西此剧本也会重启
vim apache1.yml vim apache1.yml 修改此剧本#四.处理失败任务#
#1.ignore_errors#
#作用: 当play遇到任务失败是会终止
ignore_errors: yes 将会忽略任务失败使下面的任务继续运行
实例:
vim apache1.yml 剧本中没有http这个软件,所以执行到install httpd失败
vim apache1.yml#2.force_handlers#
#作用: #当任务失败后play被终止也会调用触发器进程
vim apache1.yml 将剧本端口修改为8080
vim apache1.yml
vim apache1.yml
#3.changed_when#
#作用: #控制任务在何时报告它已进行更改
vim apache1.yml changed_when=yes强制改变的状态即使没有修改它也是改变的状态
vim apache1.yml changed_when=no 没修改就不会进行改变 #4.failed_when#
#当符合条件时强制任务失败
vim apache1.yml failed_when=yes 即使没失败也会执行错误vim apache1.yml failed_when=no 失败就是失败,没失败就是没失败#5.block#
block: ##定义要运行的任务
rescue: ##定义当block句子中出现失败任务后运行的任务
always: ##定义最终独立运行的任务
实例
vim apache2.yml
vim apache2.yml #测试练习#
建立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上
[devops@westoslinux .ansible]$ vim device2.yml
查看结果
注:模块查询方法