Anisble中的任务执行控制
目录
一、循环
1、简单循环
使用loop赋值列表的格式:
loop: ##赋值列表
- value1
- value2
- ...
{{item}} ##迭代变量名称
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
二、条件判定
1、when条件语句
此条件代表两者都满足时
when:
- 条件1
- 条件2
此条件代表两者满足一个即可
when:
- 条件1
or
条件2
2、条件判断
= | value == "字符串“,value == 数字 |
< | value < 数字 |
> | value > 数字 |
<= | value <= 数字 |
>= | value >= 数字 |
!= | value != 数字 |
is defined | 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列表中) |
上述的应用实例:
使用loop when 创建用户
vim user.yml
- name: create user
hosts: all
vars:
userlist:
- name: user1
id: 6666
comment: user1 comment
- name: user2
id: 7777
tasks:
- name: create user2
user:
name: "{{item.name}}"
uid: "{{item.id}}"
comment: "{{item.comment}}"
state: present
when:
item.comment is defined
loop:
"{{userlist}}"
- name: create user
user:
name: "{{item.name}}"
uid: "{{item.id}}"
state: present
when:
item.comment is not defined
loop:
"{{userlist}}"
ansible-playbook user.yml -C
三、触发器
notify
:触发器当遇到更改时触发handlers
handlers
:触发器触发后执行的动作
示例:
使用触发器 j2 loop 安装httpd服务并运行虚拟主机
vim web.yml
webs:
- doc: /var/www/html
index: "www.westos.org's page"
- name: bbs.westos.org
doc: /var/www/virtual/westos.org/bbs/html
index: "bbs.westos.org's page"
- name: login.westos.org
doc: /var/www/virtual/westos.org/login/html
index: "login.westos.org's page"
vim vhosts.conf.j2
{% for web in webs %}
{% if web.name is defined %}
<VirtualHost *:80>
ServerName {{web.name}}
{% endif %}
{% if web.name is not defined %}
<VirtualHost _default_:80>
{% endif %}
DocumentRoot {{web.doc}}
</VirtualHost>
{% endfor %}
vim httpd.yml
- name: create virtualhost
hosts: all
vars_files: ./web.yml
tasks:
- name: yum
yum:
name: httpd
state: present
- name: service
service:
name: httpd
state: started
enabled: yes
- name: create doc
lineinfile:
path: "{{item.doc}}/index.html"
line: "{{item.index}}"
create: yes
loop: "{{webs}}"
- name: create vhosts.conf
template:
src: ./vhosts.conf.j2
dest: /etc/httpd/conf.d/vhost.conf
notify: restart httpd
handlers:
- name: restart httpd
service:
name: httpd
state: restarted
触发器工作
因为没有文件修改触发器没有触发所以不进行重启服务
四、 处理失败任务
1、ignore_errors
作用:当play遇到任务失败时会终止,ignore_errors: yes
将会忽略任务失败使下面的任务继续运行
例子:
vim test.yml
- name: test
hosts: all
tasks:
- name: check
shell:
test -e /mnt/file
register: output
ignore_errors: yes
- name: show
debug:
msg: "{{output.rc}}"
2、force_handlers
作用:当任务失败后play被终止也会调用触发器进程
例子:
vim test.yml
- name: test
hosts: all
force_handlers: yes
tasks:
- name: create file
copy:
dest: /mnt/testfile
content: hello ale
notify: show messgaes
- name: check
shell:
test -e /mnt/file
register: output
- name: show
debug:
msg: "{{output.rc}}"
handlers:
- name: show messgaes
debug:
msg: /mnt/testfile is changed !!
3、changed_when
作用:控制任务在何时报告它已进行更改,抑制更改
例子:
vim test.yml
- name: test
hosts: all
force_handlers: yes
tasks:
- name: create file
copy:
dest: /mnt/testfile
content: hello ale
changed_when: true
notify: show messgaes
- name: check
shell:
test -e /mnt/file
register: output
- name: show
debug:
msg: "{{output.rc}}"
handlers:
- name: show messgaes
debug:
msg: /mnt/testfile is changed !!
当:changed_when: false
时
4、failed_when
作用:当符合条件时强制任务失败,但不会更改任务本身的行为
例子:
vim test.yml
- name: test
hosts: all
force_handlers: yes
tasks:
- name: create file
copy:
dest: /mnt/testfile
content: hello hahahaha
failed_when: true
notify: show messgaes
- name: check
shell:
test -e /mnt/file
register: output
- name: show
debug:
msg: "{{output.rc}}"
handlers:
- name: show messgaes
debug:
msg: /mnt/testfile is changed !!
5、block
block
:定义要运行的任务rescue
:定义当block
句子中出现失败任务后运行的任务always
:定义最终独立运行的任务
测试练习:
vim user.yml
- name: create user
hosts: all
vars:
passwd: "123"
tasks:
- name:
block:
- name:
shell:
id user1
- name: show user is exist
debug:
msg: user1 is exist
- name:
user:
name: user1
password: "{{passwd|password_hash('sha512')}}"
rescue:
- name: show user1 is not exist
debug:
msg: user1 is not exist
- name: create user1
user:
name: user1
state: present
password: "{{passwd|password_hash('sha512')}}"
always:
- name:
user:
name: user1
comment: test user1
不存在时:
存在时:
测试2:
建立playbook ~/westos.yml要求如下:
建立大小为1500M名为/dev/sdc1的设备
如果/dev/sdc不存在请输入: /dev/sdc is not exist
如果/dev/sdc大小不足1.G请输出: /dev/vdb is less then 1.5G
并建立800M大小的/dev/vdb1 此设备挂载到/westos
vim dev.yml
- name: create /dev/sdc1
hosts: all
tasks:
- block:
- name: Create a new primary partition with a size of 1.5GiB
parted:
device: /dev/sdc
number: 1
state: present
part_end: 1.5GiB
when: ansible_facts['devices']['sdc'] is defined
rescue:
- name: /dev/sdc is less then 1.5GiB
debug:
msg: /dev/sdc is less then 1.5GiB
- name: Create a new primary partition with a size of 800MiB
parted:
device: /dev/sdc
number: 1
state: present
part_end: 800MiB
always:
- name: Create a ext4 filesystem on /dev/sdb1
filesystem:
fstype: ext4
dev: /dev/sdc1
opts: -cc
- name: Mount
mount:
path: /westos
src: /dev/sdc1
fstype: ext4
state: mounted
- name:
debug:
msg: /dev/sdc is not exist
when: ansible_facts['devices']['sdc'] is not defined