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控制的更多相关文章

  1. ansible基础-task控制

    1. 前言 很多情况下,一个play是否执行会依赖于某个(些)变量的值,这个变量可以来自自定义变量.facts,甚至是另一个task的执行结果. ansible通过变量判定task是否执行,我们称之为 ...

  2. ansible 流程控制

    ansible 流程控制 使用when判断主机名 - hosts: rsync_server tasks: - name: Install rsyncd Server yum: name: rsync ...

  3. Ansible--04 ansible 流程控制

    ansible 流程控制 playbook 条件语句 不管是 shell 还是各大编程预言中,流程控制,条件判断都是必不可少的,在我们使用 Ansible的过程中,条件判断的使用频率都非常高. 例如: ...

  4. Ansible流程控制

    Ansible流程控制 数据库操作问题: 数据库的操作问题,python需要依耐的模块MySQL-python . 数据库的操作 # 设置root的密码在,root的密码设置之后,创建用户和创建数据库 ...

  5. Ansible_自动化运维《Ansible之初识-1》

    1.Ansible简介 1.1 Ansible介绍 Ansible 是一个简单的自动化运维管理工具,基于Python开发,集合了众多运维工具(puppet.cfengine.chef.func.fab ...

  6. Ansible系列(六):各种变量定义方式和变量引用

    本文目录:1.1 ansible facts1.2 变量引用json数据的方式 1.2.1 引用json字典数据的方式 1.2.2 引用json数组数据的方式 1.2.3 引用facts数据1.3 设 ...

  7. ansible基础-理解篇

    1. 介绍 要说现在的部署工具,ansible可以说家喻户晓了. ansible是一个开源软件,用于软件供应.配置管理.应用部署.ansible可以通过SSH.remote PowerShell.其他 ...

  8. ansible 自动化管理

    1 什么样的情形需要批量部署 1.操作系统的安装 常见的有collber,red hat satelite(redhat)系统专用. 2.操作系统的配置 常见的有cfengine,puppet,che ...

  9. Ansible系列(五):各种变量定义方式和变量引用

    Ansible系列文章:http://www.cnblogs.com/f-ck-need-u/p/7576137.html 1.1 ansible facts facts组件是用来收集被管理节点信息的 ...

随机推荐

  1. 基于Docker快速搭建ELK

    [摘要] 本文基于自建的Docker平台速搭建一套完整的ELK系统,相关的镜像直接从Docker Hub上获取,可以快速实现日志的采集和分析检索. 准备镜像 l  获取ES镜像:docker pull ...

  2. IOS开发中制作属于自己的静态库.a、资源库.bundle、.framework

    一.什么是库        库实际上是一种代码共享的方式,主要用于代码重用和源码隐藏,通常分为动态库和静态库. 静态库:链接时完整的拷贝至可执行文件中,被多次使用就有多份冗余拷贝. 动态库:链接时不复 ...

  3. git log详细使用参数

    1. 可以看到fileName相关的commit记录 git log filename 2. 可以显示每次提交的diff git log -p filename 3. 只看某次提交中的某个文件变化,可 ...

  4. [TimLinux] JavaScript 引用类型——Date

    1. Date var now = new Date(); // 不传参数,获取当前日期.时间. now.getDay(); // 日期 now.getMonth(); // 月份 now.getFu ...

  5. leaflet图斑历史时空播放(附源码下载)

    前言 leaflet 入门开发系列环境知识点了解: leaflet api文档介绍,详细介绍 leaflet 每个类的函数以及属性等等 leaflet 在线例子 leaflet 插件,leaflet ...

  6. seaborn 数据可视化(一)连续型变量可视化

    一.综述 Seaborn其实是在matplotlib的基础上进行了更高级的API封装,从而使得作图更加容易,图像也更加美观,本文基于seaborn官方API还有自己的一些理解.   1.1.样式控制: ...

  7. C和C++从零开始系列(一)

    今天开始写下一系列C和C++从入门开始的文章. 简单说几句C和C++的关系.C语言早于C++. C语言 贝尔实验室的Ken Thompson发明了 UNIX,当时有个B语言的.后来D.M.Ritchi ...

  8. Spring Boot 外部化配置(一)- Environment、ConfigFileApplicationListener

    目录 前言 1.起源 2.外部化配置的资源类型 3.外部化配置的核心 3.1 Environment 3.1.1.ConfigFileApplicationListener 3.1.2.关联 Spri ...

  9. 笔记||Python3之对象的方法

    什么是对象的方法? python中的一切类型的数据都是对象. 对象:数据和方法 对象数据:如 a = 'sfd' 对象方法:其实就是属于该对象的函数 对象的方法调用:对象.方法 字符串对象常用的方法: ...

  10. BIOS安全设置

    1.开机按F2进入BIOS 2.进入 Security 界面 3.Set user password 用户密码 开机密码 设置为123456 4.Set supervisor password 进BI ...