ansible_playbook任务控制

ansible_playbook任务控制:
条件判断when
循环语句with_items
触发器 handlers
标签tags 根据指定的标签执行,调试
包含include
错误忽略ignore_errors
错误处理 changed_when

1.条件判断when
使用()
变量不使用{{}}

自带的变量名字获取:
ansible servers -i hosts.cfg -m setup

1)示例:当系统为CentOS时复制文件
- hosts: servers
tasks:
- name: change configure file when is CentOS
template: src=./test.test dest=/root
when : ( ansible_distribution == "CentOS" )

命令:ansible-playbook -i hosts.cfg when.yaml

2)示例:hostname包含"iZ2zei0"时复制文件(给特定主机名的主机)
- hosts: servers
tasks:
- name: change configure file when is iZ2zei0*
template: src=./test.test dest=/root
when: ( ansible_fqdn is match ("iZ2zei0*") )

#多个主机名( 使用或者 or)
- hosts: servers
tasks:
- name: change configure file when is iZ2zei0*
template: src=./test.test dest=/root
when: ( ansible_fqdn is match ("iZ2zei0*") ) or ( ansible_fqdn is match ("ali-*") )

 

2.循环语句with_items
1)示例:使用方法一
- hosts: servers
tasks:
- name: systemctl restart {{ item }}
serverice: name={{ item }} state=restarted
with_items:
- nginx
- php

2)示例:使用方法二
批量创建用户
- hosts: servers
tasks:
- name: Create User
user: name={{ item.name }} group={{ item.group }} state=present
with_items:
- { name: 'test111', group: 'bin'}
- { name: 'test222', group: 'bin'}


命令:ansible-playbook -i hosts.cfg with_items.yaml

 


3.触发器 handlers(所有任务都执行完,最后执行)
notify监控 --> handlers 触发

handlers与tasks同级

1)示例:配置文件改变,则重启nginx服务
- hosts: servers
tasks:
- name: change configure for nginx
template: src=./nginx.conf dest=/root
notify: systemctl restart nginx
handlers:
- name: systemctl restart nginx
service: name=nginx state=restarted

命令:ansible-playbook -i hosts.cfg handlers.yaml

2)示例:配置文件改变,则重启多个服务
- hosts: servers
tasks:
- name: change configure for nginx
template: src=./nginx.conf dest=/root
notify:
- systemctl restart nginx
- systemctl restart php-fpm

handlers:
- name: systemctl restart nginx
service: name=nginx state=restarted
- name: systemctl restart php-fpm
service: name=php-fqm state=restarted

 

4.标签tags 根据指定的标签执行,调试

对一个任务指定一个标签
对一个任务指定多个标签
对多个任务指定一个标签

 

1)示例:执行某个任务
- hosts: servers
tasks:
- name: change configure for nginx
template: src=./nginx.conf dest=/root
notify: systemctl restart nginx
tags: check_nginx_configure
handlers:
- name: systemctl restart nginx
service: name=nginx state=restarted

命令:ansible-playbook -i hosts.cfg tags.yaml -t "check_nginx_configure"

2)示例:除了某个任务都执行(忽略某个任务)

命令:ansible-playbook -i hosts.cfg tags.yaml --skip-tags "check_nginx_configure"

 

5. 包含include(与nginx类似)
1)示例:部分包含
- hosts: servers
tasks:
- name: change configure for nginx
include: include.yaml


2)示例:全包含
- import_playbook: tags.yaml
- import_playbook: with_items.yaml


6. 错误忽略ignore_errors
有报错会终止脚本
加上这个会忽略掉此任务,接着执行下面的
1)示例:
- hosts: servers
tasks:
- name: Shella
shell: /bin/false
ignore_errors: yes
- name: Shellb
shell: ls /root

命令:ansible-playbook -i hosts.cfg ignore_errors.yaml


7.错误处理 changed_when

1)示例:强制执行handlers(不常用)
- hosts: servers
force_handlers: yes
tasks:
- name: change configure for nginx
template: src=./nginx.conf dest=/root
notify: systemctl restart nginx
handlers:
- name: systemctl restart nginx
service: name=nginx state=restarted

2)changed_when发现则执行,否则不继续执行
- hosts: servers
tasks:
- name: change configure for nginx
template: src=./nginx.conf dest=/root
notify: systemctl restart nginx

- name: check configure for nginx
shell: nginx -t
register: check_nginx
changed_when: ( check_nginx.stdout.find('successful') )
handlers:
- name: systemctl restart nginx
service: name=nginx state=restarted


命令:ansible-playbook -i hosts.cfg changed_when.yaml

 

posted @ 2024-09-22 23:39  铿锵有力自信且坚定  阅读(29)  评论(0)    收藏  举报