Ansible-任务执行控制
一.循环
循环迭代任务
1.简单循环
loop: ##赋值列表
- value1
- value2
- ...
{<!-- -->{item}} ##迭代变量名称
[admin@ansible .ansible]$ ansible westos -m shell -a 'touch /mnt/westos_file2'
172.25.32.11 | CHANGED | rc=0 >>
172.25.32.12 | CHANGED | rc=0 >>
[admin@ansible .ansible]$ cat westos.yml
- name: create file
hosts: westos
tasks:
- name: file module
file:
name: /mnt/{<!-- -->{item}}
state: absent
loop:
- westos_file1
- westos_file2
[admin@ansible .ansible]$ ansible-playbook westos.yml
PLAY [create file] ********************************************************************************************************
TASK [Gathering Facts] ****************************************************************************************************
ok: [172.25.32.12]
ok: [172.25.32.11]
TASK [file module] ********************************************************************************************************
ok: [172.25.32.12] => (item=westos_file1)
ok: [172.25.32.11] => (item=westos_file1)
changed: [172.25.32.11] => (item=westos_file2)
changed: [172.25.32.12] => (item=westos_file2)
PLAY RECAP ****************************************************************************************************************
172.25.32.11 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
172.25.32.12 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
2.循环散列或字典列表
[admin@ansible .ansible]$ cat westos1.yml
- name: create file
hosts: westos
tasks:
- name: file module
service:
name: "{<!-- -->{ item.name}}"
state: "{<!-- -->{ item.state }}"
loop:
- name: httpd
state: stopped
- name: vsftpd
state: started
[admin@ansible .ansible]$ ansible-playbook westos1.yml
PLAY [create file] ********************************************************************************************************
TASK [Gathering Facts] ****************************************************************************************************
ok: [172.25.32.12]
ok: [172.25.32.11]
TASK [file module] ********************************************************************************************************
changed: [172.25.32.12] => (item={u'state': u'stopped', u'name': u'httpd'})
changed: [172.25.32.11] => (item={u'state': u'stopped', u'name': u'httpd'})
changed: [172.25.32.11] => (item={u'state': u'started', u'name': u'vsftpd'})
changed: [172.25.32.12] => (item={u'state': u'started', u'name': u'vsftpd'})
PLAY RECAP ****************************************************************************************************************
172.25.32.11 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
172.25.32.12 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
二.条件
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列表中
多条条件组合
when:
条件1 and 条件2
- 条件1
- 条件2
when:
条件1 or 条件2
when: >
条件1
or
条件2
测试admin用户是否存在
[admin@ansible .ansible]$ cat westos.yml
- name: test
hosts: westos
tasks:
- name: check user
shell: id admin
ignore_errors: yes
register: admin
- name: debug
debug:
msg: admin user is exist
when:
- admin.rc == 0
[admin@ansible .ansible]$ ansible-playbook westos.yml
PLAY [test] ***************************************************************************************************************
TASK [Gathering Facts] ****************************************************************************************************
ok: [172.25.32.12]
ok: [172.25.32.11]
TASK [check user] *********************************************************************************************************
changed: [172.25.32.11]
changed: [172.25.32.12]
TASK [debug] **************************************************************************************************************
ok: [172.25.32.11] => {
"msg": "admin user is exist"
}
ok: [172.25.32.12] => {
"msg": "admin user is exist"
}
PLAY RECAP ****************************************************************************************************************
172.25.32.11 : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
172.25.32.12 : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
三.触发器
notify: 触发器当遇到更改是触发handlers
handlers: 触发器触发后执行的动作
注意:触发器没有遇到更改时就不会触发也就不会执行下边的动作,遇到更改时会触发且执行下边的动作
[admin@ansible .ansible]$ cat apache_port.yml
- name: port check
hosts: westos
tasks:
- name: configure httpd.conf
lineinfile:
path: /etc/httpd/conf/httpd.conf
regexp: "^Listen"
line: "Listen 8080"
notify: restart httpd
handlers:
- name: restart httpd
service:
name: httpd
state: restarted
- name: restart vsftpd
service:
name: vsftpd
state: restarted
[admin@ansible .ansible]$ ansible-playbook apache_port.yml
PLAY [port check] *********************************************************************************************************
TASK [Gathering Facts] ****************************************************************************************************
ok: [172.25.32.12]
ok: [172.25.32.11]
TASK [configure httpd.conf] ***********************************************************************************************
changed: [172.25.32.12]
changed: [172.25.32.11]
RUNNING HANDLER [restart httpd] *******************************************************************************************
changed: [172.25.32.12]
changed: [172.25.32.11]
PLAY RECAP ****************************************************************************************************************
172.25.32.11 : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
172.25.32.12 : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
被控主机查看端口
[root@www ~]# cat /etc/httpd/conf/httpd.conf
......
#Listen 12.34.56.78:80
Listen 8080
......
四.处理失败任务
1.ignore_errors
作用:
当play遇到任务失败是会终止
ignore_errors: yes 将会忽略任务失败使下面的任务继续运行
[admin@ansible .ansible]$ cat ignore_errors.yml
- name: test
hosts: westos
tasks:
- name: test play
dnf:
name: westos
state: latest
ignore_errors: yes
- name: create file
file:
path: /mnt/file
state: touch
[admin@ansible .ansible]$ ansible-playbook ignore_errors.yml
PLAY [test] ***************************************************************************************************************
TASK [Gathering Facts] ****************************************************************************************************
ok: [172.25.32.12]
ok: [172.25.32.11]
TASK [test play] **********************************************************************************************************
fatal: [172.25.32.12]: FAILED! => {"changed": false, "cmd": "dnf install -y python2-dnf", "msg": "[Errno 2] No such file or directory", "rc": 2}
...ignoring
fatal: [172.25.32.11]: FAILED! => {"changed": false, "cmd": "dnf install -y python2-dnf", "msg": "[Errno 2] No such file or directory", "rc": 2}
...ignoring
TASK [create file] ********************************************************************************************************
changed: [172.25.32.11]
changed: [172.25.32.12]
PLAY RECAP ****************************************************************************************************************
172.25.32.11 : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=1
172.25.32.12 : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=1
2.force_handlers
作用: 当任务失败后play被终止也会调用触发器进程
- name: port check
hosts: westos
force_handlers: yes
tasks:
- name: configure httpd.conf
lineinfile:
path: /etc/httpd/conf/httpd.conf
regexp: "^Listen"
line: "Listen 8080"
notify:
- restart httpd
- restart vsftpd
- name: install error
dnf:
name: westos
state: latest
handlers:
- name: restart httpd
service:
name: httpd
state: restarted
- name: restart vsftpd
service:
name: vsftpd
state: restarted
[admin@ansible .ansible]$ ansible-playbook apache_port.yml
PLAY [port check] *********************************************************************************************************
TASK [Gathering Facts] ****************************************************************************************************
ok: [172.25.32.12]
ok: [172.25.32.11]
TASK [configure httpd.conf] ***********************************************************************************************
changed: [172.25.32.11]
changed: [172.25.32.12]
TASK [install error] ******************************************************************************************************
fatal: [172.25.32.11]: FAILED! => {"changed": false, "cmd": "dnf install -y python2-dnf", "msg": "[Errno 2] No such file or directory", "rc": 2}
fatal: [172.25.32.12]: FAILED! => {"changed": false, "cmd": "dnf install -y python2-dnf", "msg": "[Errno 2] No such file or directory", "rc": 2}
RUNNING HANDLER [restart httpd] *******************************************************************************************
changed: [172.25.32.12]
changed: [172.25.32.11]
RUNNING HANDLER [restart vsftpd] ******************************************************************************************
changed: [172.25.32.11]
changed: [172.25.32.12]
PLAY RECAP ****************************************************************************************************************
172.25.32.11 : ok=4 changed=3 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
172.25.32.12 : ok=4 changed=3 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
3.changed_when
作用: 控制任务在何时报告它已进行更改
[admin@ansible .ansible]$ cat apache_port.yml
- name: port check
hosts: westos
force_handlers: yes
tasks:
- name: configure httpd.conf
lineinfile:
path: /etc/httpd/conf/httpd.conf
regexp: "^Listen"
line: "Listen 8080"
changed_when: yes
notify:
- restart httpd
- restart vsftpd
handlers:
- name: restart httpd
service:
name: httpd
state: restarted
- name: restart vsftpd
service:
name: vsftpd
state: restarted
不论文件有没有作修改,他都会认为文件已修改,且执行。
[admin@ansible .ansible]$ ansible-playbook apache_port.yml
PLAY [port check] *********************************************************************************************************
TASK [Gathering Facts] ****************************************************************************************************
ok: [172.25.32.12]
ok: [172.25.32.11]
TASK [configure httpd.conf] ***********************************************************************************************
changed: [172.25.32.12]
changed: [172.25.32.11]
RUNNING HANDLER [restart httpd] *******************************************************************************************
changed: [172.25.32.12]
changed: [172.25.32.11]
RUNNING HANDLER [restart vsftpd] ******************************************************************************************
changed: [172.25.32.12]
changed: [172.25.32.11]
PLAY RECAP ****************************************************************************************************************
172.25.32.11 : ok=4 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
172.25.32.12 : ok=4 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
4.failed_when
当符合条件时强制任务失败
[admin@ansible .ansible]$ cat ignore_errors.yml
- name: test
hosts: westos
force_handlers: yes
tasks:
- name: test play
dnf:
name: westos
state: latest
failed_when: no
- name: create file
file:
path: /mnt/file1
state: touch
不论任务执行成功与否,都视为成功。
[admin@ansible .ansible]$ ansible-playbook ignore_errors.yml
PLAY [test] ***************************************************************************************************************
TASK [Gathering Facts] ****************************************************************************************************
ok: [172.25.32.12]
ok: [172.25.32.11]
TASK [test play] **********************************************************************************************************
ok: [172.25.32.11]
ok: [172.25.32.12]
TASK [create file] ********************************************************************************************************
changed: [172.25.32.12]
changed: [172.25.32.11]
PLAY RECAP ****************************************************************************************************************
172.25.32.11 : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
172.25.32.12 : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
5.block
block: ##定义要运行的任务
rescue: ##定义当block句子中出现失败任务后运行的任务
always: ##定义最终独立运行的任务
当任务中block执行没问题,之后执行always。当block有问题则执行rescue,然后执行always。也就是说不论执行的是block还是rescue,之后都会执行always。
[admin@ansible .ansible]$ cat westos.yml
- name: test
hosts: westos
tasks:
- name: test play
block:
- name: install httpd
yum:
name: westos
state: latest
rescue:
- name: install vsftpd
yum:
name: vsftpd
state: latest
always:
- name: hello westos
debug:
msg: hello westos
当block执行失败时,执行rescue,之后执行always
[admin@ansible .ansible]$ ansible-playbook westos.yml
PLAY [test] ***************************************************************************************************************
TASK [Gathering Facts] ****************************************************************************************************
ok: [172.25.32.12]
ok: [172.25.32.11]
TASK [install httpd] ******************************************************************************************************
fatal: [172.25.32.11]: FAILED! => {"changed": false, "msg": "No package matching 'westos' found available, installed or updated", "rc": 126, "results": ["No package matching 'westos' found available, installed or updated"]}
fatal: [172.25.32.12]: FAILED! => {"changed": false, "msg": "No package matching 'westos' found available, installed or updated", "rc": 126, "results": ["No package matching 'westos' found available, installed or updated"]}
TASK [install vsftpd] *****************************************************************************************************
ok: [172.25.32.11]
ok: [172.25.32.12]
TASK [hello westos] *******************************************************************************************************
ok: [172.25.32.11] => {
"msg": "hello westos"
}
ok: [172.25.32.12] => {
"msg": "hello westos"
}
PLAY RECAP ****************************************************************************************************************
172.25.32.11 : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=1 ignored=0
172.25.32.12 : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=1 ignored=0
[admin@ansible .ansible]$ cat westos.yml
- name: test
hosts: westos
tasks:
- name: test play
block:
- name: install httpd
yum:
name: westos
state: latest
failed_when: no
rescue:
- name: install vsftpd
yum:
name: vsftpd
state: latest
always:
- name: hello westos
debug:
msg: hello westos
当block执行成功时,跳过rescue,然后执行always
[admin@ansible .ansible]$ ansible-playbook westos.yml
PLAY [test] ***************************************************************************************************************
TASK [Gathering Facts] ****************************************************************************************************
ok: [172.25.32.12]
ok: [172.25.32.11]
TASK [install httpd] ******************************************************************************************************
ok: [172.25.32.12]
ok: [172.25.32.11]
TASK [hello westos] *******************************************************************************************************
ok: [172.25.32.11] => {
"msg": "hello westos"
}
ok: [172.25.32.12] => {
"msg": "hello westos"
}
PLAY RECAP ****************************************************************************************************************
172.25.32.11 : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
172.25.32.12 : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
#测试练习#
建立playbook ~/westos.yml要求如下:
建立大小为1500M名为/dev/vdb1的设备
如果/dev/vdb不存在请输入:
/dev/vdb is not exist
如果/dev/vdb大小不足2G请输出:
/dev/vdb is less then 2G
并建立800M大小的/dev/vdb1
此设备挂载到/westos上
[admin@ansible .ansible]$ cat westos.yml
- name: create lvm
hosts: all
tasks:
- name: check /dev/vdb
debug:
msg: /dev/vdb is not exist
when: ansible_facts['devices']['vdb'] is not defined
- name: create /dev/vdb1
block:
- name: create 1500M
parted:
device: /dev/vdb
number: 1
state: present
part_end: 1500MB
when: ansible_facts['devices']['vdb'] is defined
rescue:
- name: create 800MB
parted:
device: /dev/vdb
number: 1
state: present
part_end: 800MB
when: ansible_facts['devices']['vdb'] is defined
always:
- name: mkfs.xfs /dev/vdb1
filesystem:
fstype: xfs
dev: /dev/vdb1
- name: mkdir /westos
file:
path: /westos
state: directory
- name: mount /dev/vdb1
mount:
path: /westos
src: /dev/vdb1
fstype: xfs
state: mounted
when: ansible_facts['devices']['vdb'] is defined
[admin@ansible .ansible]$ ansible-playbook westos.yml
PLAY [create lvm] *********************************************************************************************************
TASK [Gathering Facts] ****************************************************************************************************
ok: [172.25.32.12]
ok: [172.25.32.11]
TASK [check /dev/vdb] *****************************************************************************************************
ok: [172.25.32.11] => {
"msg": "/dev/vdb is not exist"
}
ok: [172.25.32.12] => {
"msg": "/dev/vdb is not exist"
}
TASK [create 1500M] *******************************************************************************************************
skipping: [172.25.32.11]
skipping: [172.25.32.12]
TASK [mkfs.xfs /dev/vdb1] *************************************************************************************************
fatal: [172.25.32.11]: FAILED! => {"changed": false, "msg": "Device /dev/vdb1 not found."}
fatal: [172.25.32.12]: FAILED! => {"changed": false, "msg": "Device /dev/vdb1 not found."}
PLAY RECAP ****************************************************************************************************************
172.25.32.11 : ok=2 changed=0 unreachable=0 failed=1 skipped=1 rescued=0 ignored=0
172.25.32.12 : ok=2 changed=0 unreachable=0 failed=1 skipped=1 rescued=0 ignored=0