4.Ansible Task控制
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')
4.Ansible Task控制的更多相关文章
- ansible基础-task控制
1. 前言 很多情况下,一个play是否执行会依赖于某个(些)变量的值,这个变量可以来自自定义变量.facts,甚至是另一个task的执行结果. ansible通过变量判定task是否执行,我们称之为 ...
- ansible 流程控制
ansible 流程控制 使用when判断主机名 - hosts: rsync_server tasks: - name: Install rsyncd Server yum: name: rsync ...
- Ansible--04 ansible 流程控制
ansible 流程控制 playbook 条件语句 不管是 shell 还是各大编程预言中,流程控制,条件判断都是必不可少的,在我们使用 Ansible的过程中,条件判断的使用频率都非常高. 例如: ...
- Ansible流程控制
Ansible流程控制 数据库操作问题: 数据库的操作问题,python需要依耐的模块MySQL-python . 数据库的操作 # 设置root的密码在,root的密码设置之后,创建用户和创建数据库 ...
- Ansible_自动化运维《Ansible之初识-1》
1.Ansible简介 1.1 Ansible介绍 Ansible 是一个简单的自动化运维管理工具,基于Python开发,集合了众多运维工具(puppet.cfengine.chef.func.fab ...
- Ansible系列(六):各种变量定义方式和变量引用
本文目录:1.1 ansible facts1.2 变量引用json数据的方式 1.2.1 引用json字典数据的方式 1.2.2 引用json数组数据的方式 1.2.3 引用facts数据1.3 设 ...
- ansible基础-理解篇
1. 介绍 要说现在的部署工具,ansible可以说家喻户晓了. ansible是一个开源软件,用于软件供应.配置管理.应用部署.ansible可以通过SSH.remote PowerShell.其他 ...
- ansible 自动化管理
1 什么样的情形需要批量部署 1.操作系统的安装 常见的有collber,red hat satelite(redhat)系统专用. 2.操作系统的配置 常见的有cfengine,puppet,che ...
- Ansible系列(五):各种变量定义方式和变量引用
Ansible系列文章:http://www.cnblogs.com/f-ck-need-u/p/7576137.html 1.1 ansible facts facts组件是用来收集被管理节点信息的 ...
随机推荐
- java前端与后端怎么选??
想做这个行业,就应该了解职能以及技能需求,这样学习才能更高效.我知道一些刚刚入行的小伙伴不清楚前端.后端.到底指的是什么?两者直接的区别 前端开发 前端开发主要涉及网站和App,用户能够从浏览器上或A ...
- 还在用背单词App?使用Python开发英语单词自测工具,助你逆袭单词王!
学英语广告 最近也许是刚开学的原因,不管是公众号,还是刷抖音,导出都能看到关于学英语.背单词的广告. 不知道现在学生们背单词买的什么辅导材料.反正我们上学那会,<星火阅读>特别的火.记得当 ...
- 工作流Activity组件值数据传递获取问题
如图:先简单说一下大致过程 通过具体的菜单节点,加载具体的指令组件.本着低耦合的原件,需要将核查组件从指令组件重拆分出来,作为单独的组件根据业务需要拖拽到指令组件中.但是具体业务方面核查组件一方面需要 ...
- Spring bean 初始化失败
在一个*context.xml 配置文件 A 中, 有个定义的bean B, 把 A 添加到 application-context.xml 中,发现B不能正常初始化. 解决办法: 添加 <co ...
- React、React Native面试题
1.React Native相对于原生的ios和Android有哪些优势. react native一套代码可以开发出跨平台app, 减少了人力.节省了时间.避免了 iOS 与 Android 版本发 ...
- UVA-156
Most crossword puzzle fans are used to anagrams - groups of words with the same letters in different ...
- Python爬虫爬取异步加载的数据
前言 本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理.作者:努力努力再努力 爬取qq音乐歌手数据接口数据 https://y.qq ...
- 分布式算法-一致性HASH
分布式算法 参考: https://blog.51cto.com/alanwu/1431397 https://blog.csdn.net/kojhliang/article/details/8120 ...
- debian官网qcow2镜像修改root账号密码,开启ssh等
1.下载官网qcow2镜像文件 wget http://172.16.20.10/vmtemplate/KVM/wangrui/Debian/debian-10.2.0-openstack-amd64 ...
- 手动SQL注入原理分析与实践
代码仓库 本文所用代码的代码库地址: 点击这里前往Github仓库 了解SQL注入 定义 SQL注入攻击(SQL Injection),简称注入攻击,是Web开发中最常见的一种安全漏洞.可以用它来从数 ...