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. 配置aria2

    Mac 用户肯定都受够了百度网盘在自己电脑上的糟糕体验,至少我是如此:安装官方的 App,经常下载时中断,有时甚至 Bug 般连续中断,无奈使用浏览器下载,速度却是令人挠头.花点时间来配置 aria2 ...

  2. STM32 GPIO口的配置和应用

    STM32F103ZET6 一共有7组IO口(有FT的标识是可以识别5v的) 每组IO口有16个IO 一共16*7=112个IO 4种输入模式: (1) GPIO_Mode_AIN 模拟输入 (2) ...

  3. 【云速建站】微信公众平台中维护IP白名单

    [摘要] 介绍获取接入IP白名单的操作步骤 网站后台对接微信公众号.支付等都依赖于白名单,接下来就介绍一下白名单的配置. 1.1      为什么要设置白名单 为了提高公众平台开发者接口调用的安全性, ...

  4. RS485与RS232

    以下内容为结合视频,加上自述对其理解. 信息在传输线上通过电压信息进行传输,一个字节的数据有8位. 当传输一个字节的信息时,通信方式有串行通信与并行通信,在这两种通信方式之中,RS485是并行通信,R ...

  5. 自学python中的心得

    以后的日子里我将与可爱的亲们一起度过我自学python的岁月,请博客园里的大佬们监督与见证.

  6. ASP.NET Core 选项模式源码学习Options IOptions(二)

    前言 上一篇文章介绍IOptions的注册,本章我们继续往下看 IOptions IOptions是一个接口里面只有一个Values属性,该接口通过OptionsManager实现 public in ...

  7. kubernetes学习笔记(一)——minikube安装记录

    想学习一下kubernetes,于是先安装一个单机版来学习一下.但是就是这个最简单的单机版安装方式都倒腾了我好久,记录下自己的安装过程.博主是在windows利用vmware workstation安 ...

  8. JavaEE基础(03):Http请求详解,握手挥手流程简介

    本文源码:GitHub·点这里 || GitEE·点这里 一.Http协议简介 1.概念说明 HTTP超文本传输协议,是用于从万维网服务器传输超文本到本地浏览器的传送协议,基于TCP/IP通信协议来传 ...

  9. Docker系列-(2) 镜像制作与发布

    上篇文章引入了Docker的基本原理和操作,本节文章主要介绍如何制作Docker镜像和发布. 镜像文件结构 Docker镜像的本质是一系列文件的集合,这些文件依次叠加,形成了最后的镜像文件,类似于下图 ...

  10. 重新精读《Java 编程思想》系列之组合与继承

    Java 复用代码的两种方式组合与继承. 组合 组合只需将对象引用置于新类中即可. 比如我们有一个B类,它具有一个say方法,我们在A类中使用B类的方法,就是组合. public class B { ...