1.tag标签(调试)
--skip-tags install_nfs 跳过此标签
-t 指定标签名
[root@manager tasks]# cat task_nfs.yml
- hosts: webservers
tasks:
#对一个任务打多个标签
- name: Install Nfs Server
yum:
name: nfs-utils
state: present
tags:
- install_nfs
- install_nfs-server
#对一个任务打一个标签
- name: Service Nfs Server
service:
name: nfs-server
state: started
enabled: yes
tags: start_nfs-server
ansible-playbook -i ../hosts task_nfs.yml -t start_nfs-server123
ansible-playbook -i ../hosts task_nfs.yml --skip-tags install_nfs-server
2.include包含
[root@manager tasks]# cat restart_nginx.yml
- name: Restart Nginx Server
systemd:
name: nginx
state: restarted
[root@manager tasks]# cat a_project.yml
- hosts: webservers
tasks:
- name: A Project command
command: echo "A"
- name: Restart nginx
include: ./restart_nginx.yml
[root@manager tasks]# cat b_project.yml
- hosts: webservers
tasks:
- name: B Project command
command: echo "B"
- name: Restart nginx
include: ./restart_nginx.yml
3.错误处理 ignore_errors: yes
[root@manager tasks]# cat task_ignore.yml
- hosts: webservers
tasks:
#明确知道该TASK可能会报错,及时报错也希望不影响后续的TASK任务时
- name: Ignore False
command: /bin/false
ignore_errors: yes
- name: touch new file
file: path=/tmp/bgx_ignore state=touch
changed_when: false --->针对shell命令
force_handlers: yes --->强行调用handlers
4.异常处理
1.每次状态都是changed,纵使没有修改过被控端
#Check_Redis_Status=$(netstat -lntp | grep redis)
- name: Check Redis Status
shell: netstat -lntp | grep redis
register: Check_Redis_Status
changed_when: false
#echo ${Check_Redis_Status}
- name: Debug Check_Redis Variables
debug:
msg: "Redis Status: {{ Check_Redis_Status.stdout_lines }}"
2.nginx推送配置文件后,没有任何的检查功能
###low版
[root@manager tasks]# cat task_nginx.yml
- hosts: webservers
tasks:
#安装nginx
- name: Installed nginx Server
yum:
name: nginx
state: present
#配置nginx
- name: Configure nginx Server
template:
src: ./file/nginx.conf.j2
dest: /etc/nginx/nginx.conf
#检查nginx (Check_Nginx_Status=$(nginx -t))
- name: Check Nginx Configure File
shell: nginx -t
register: Check_Nginx_Status
ignore_errors: yes
changed_when: false
#启动Nginx
- name: Systemd Nginx Server
systemd:
name: nginx
state: started
enabled: yes
when: ( Check_Nginx_Status.rc == 0 ) #只有Check_Nginx_Status.rc=0时,才会执行启动操作
#####new版
[root@manager tasks]# cat task_nginx.yml
- hosts: webservers
tasks:
#安装nginx
- name: Installed nginx Server
yum:
name: nginx
state: present
#配置nginx
- name: Configure nginx Server
template:
src: ./file/nginx.conf.j2
dest: /etc/nginx/nginx.conf
#检查nginx (Check_Nginx_Status=$(nginx -t))
- name: Check Nginx Configure File
shell: nginx -t
register: Check_Nginx_Status
changed_when:
- Check_Nginx_Status.stdout.find('successful')
- false
#启动Nginx
- name: Systemd Nginx Server
systemd:
name: nginx
state: started
enabled: yes
###new版
[root@manager tasks]# cat task_nginx.yml
- hosts: webservers
tasks:
#安装nginx
- name: Installed nginx Server
yum:
name: nginx
state: present
#配置nginx
- name: Configure nginx Server
template:
src: ./file/nginx.conf.j2
dest: /etc/nginx/nginx.conf
#检查nginx (Check_Nginx_Status=$(nginx -t))
- name: Check Nginx Configure File
shell: nginx -t
#启动Nginx
- name: Systemd Nginx Server
systemd:
name: nginx
state: started
enabled: yes
3.强制调用handlers 触发器
[root@manager tasks]# cat task_nginx.yml
- hosts: webservers
force_handlers: yes #无论tasks失败与否,只要通过过handlers,那me一定会执行
tasks:
#安装nginx
- name: Installed nginx Server
yum:
name: nginx
state: present
#配置nginx
- name: Configure nginx Server
template:
src: ./file/nginx.conf.j2
dest: /etc/nginx/nginx.conf
notify: Restart Nginx Server
#检查nginx (Check_Nginx_Status=$(nginx -t))
- name: Check Nginx Configure File
shell: nginx -t
#启动Nginx
- name: Systemd Nginx Server
systemd:
name: nginx
state: started
enabled: yes
handlers:
- name: Restart Nginx Server
systemd:
name: nginx
state: restarted
总结: Ansible Task 控制
- 1.判断语句 when
- 2.循环语句 with_items
- 3.触发器 handlers
- 4.标签 tag
- 5.忽略错误 ignore_errors
- 6.异常处理
- 1.关闭TASK的changed状态 -->让该TASK一直处理OK状态 changed_when: false
- 2.强制调用handlers: 当正常task调用过handlers,则无论后续的task成功还是失败,都会调用handlers触发器执行任务.
- 3........ Check_Nginx_Status.stdout.find('successful')