1. playbook的异常处理

1.1 Playbook错误忽略

playbook 执行的过程中,难免会遇到一些错误(返回结果不为0)。由于 playbook 遇到错误后,不会执行之后的任务,不便于调试,此时,可以使用 ignore_errors 来暂时忽略错误,使得 playbook 继续执行。

  • 首先编写一个playbook文件:

    [root@xuzhichao playbook]# cat ignore.yml
    - hosts: localhost
    tasks:
    - name: Igonre False
    shell: /bin/false <==此命令返回值一定不为0 - name: Print Message
    debug:
    msg: "It's hard to here"
  • 此时运行playbook时会在Igonre False任务中报错,无法继续向下运行:

    [root@xuzhichao playbook]# ansible-playbook ignore.yml 
    
    PLAY [localhost] **********************************************************************************************************************************************
    
    TASK [Igonre False] *******************************************************************************************************************************************
    fatal: [localhost]: FAILED! => {"changed": true, "cmd": "/bin/false", "delta": "0:00:00.024018", "end": "2021-08-05 21:36:36.638534", "msg": "non-zero return code", "rc": 1, "start": "2021-08-05 21:36:36.614516", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []} PLAY RECAP ****************************************************************************************************************************************************
    localhost : ok=0 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
  • 修改playbook文件,忽略中间task的错误,继续向下执行:

    [root@xuzhichao playbook]# cat ignore.yml
    - hosts: localhost
    tasks:
    - name: Igonre False
    shell: /bin/false
    ignore_errors: yes - name: Print Message
    debug:
    msg: "It's hard to here"
  • 此时运行playbook会忽略掉中间task的错误,继续向下运行:

    [root@xuzhichao playbook]# ansible-playbook ignore.yml 
    
    PLAY [localhost] **********************************************************************************************************************************************
    
    TASK [Igonre False] *******************************************************************************************************************************************
    fatal: [localhost]: FAILED! => {"changed": true, "cmd": "/bin/false", "delta": "0:00:00.004268", "end": "2021-08-05 21:37:16.630140", "msg": "non-zero return code", "rc": 1, "start": "2021-08-05 21:37:16.625872", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}
    ...ignoring <==忽略错误 TASK [Print Message] ******************************************************************************************************************************************
    ok: [localhost] => {
    "msg": "It's hard to here"
    } PLAY RECAP ****************************************************************************************************************************************************
    localhost : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=1
  • 也可以写成下面的形式来保证task的返回值为0

    - hosts: localhost
    tasks:
    - name: Igonre False
    shell: /bin/false || /bin/ture - name: Print Message
    debug:
    msg: "It's hard to here"

1.2 task执行失败强制调用handlers

通常情况下,当 task 失败后, playbook 将会终止,任何在前面已经被 tasks notifyhandlers 都不会被执行。如果你在 playbook 中设置了 force_handlers: yes 参数,被通知的 handlers 就会被强制执行。

  • 请看下面的playbook

    [root@xuzhichao playbook]# cat force_handlers.yml
    - hosts: localhost tasks:
    - name: Copy Configure File
    template:
    src: conf/nginx.conf.j2
    dest: /tmp/nginx.conf
    notify: Start Nginx - name: Igonre False
    shell: /bin/false handlers:
    - name: Start Nginx
    service:
    name: nginx
    state: started
  • conf/nginx.conf.j2文件发生改变时,由于在 Igonre False这个task中执行出现错误,将不会执行handlers中的内容。

    [root@xuzhichao playbook]# ansible-playbook force_handlers.yml 
    
    PLAY [localhost] **********************************************************************************************************************************************
    
    TASK [Copy Configure File] ************************************************************************************************************************************
    ok: [localhost] TASK [Igonre False] *******************************************************************************************************************************************
    fatal: [localhost]: FAILED! => {"changed": true, "cmd": "/bin/false", "delta": "0:00:00.006289", "end": "2021-08-05 22:09:40.799988", "msg": "non-zero return code", "rc": 1, "start": "2021-08-05 22:09:40.793699", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []} PLAY RECAP ****************************************************************************************************************************************************
    localhost : ok=1 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
  • 若此时需要强制执行handlers中的内容,需要使用force_handlers: yes 参数,playbook修改如下:

    [root@xuzhichao playbook]# cat force_handlers.yml
    - hosts: localhost
    force_handlers: yes tasks:
    - name: Copy Configure File
    template:
    src: conf/nginx.conf.j2
    dest: /tmp/nginx.conf
    notify: Start Nginx - name: Igonre False
    shell: /bin/false handlers:
    - name: Start Nginx
    service:
    name: nginx
    state: started
  • 此时运行playbook,当conf/nginx.conf.j2文件发生改变时,handlers将会被强制执行:

    [root@xuzhichao playbook]# ansible-playbook force_handlers.yml 
    
    PLAY [localhost] **********************************************************************************************************************************************
    
    TASK [Copy Configure File] ************************************************************************************************************************************
    changed: [localhost] TASK [Igonre False] *******************************************************************************************************************************************
    fatal: [localhost]: FAILED! => {"changed": true, "cmd": "/bin/false", "delta": "0:00:00.003843", "end": "2021-08-05 22:12:00.335807", "msg": "non-zero return code", "rc": 1, "start": "2021-08-05 22:12:00.331964", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []} RUNNING HANDLER [Start Nginx] *********************************************************************************************************************************
    ok: [localhost] PLAY RECAP ****************************************************************************************************************************************************
    localhost : ok=2 changed=1 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0

1.3 控制Tasks报告状态为OK

有时一些task会在每次playbook运行是都会执行一次,这样会导致playbook的运行结果中该task会显示为changed状态,但是有时这些task并没有改变被控端的状态,仅仅是获取了某些信息,此时可以使用changed_when: false 来抑制这个状态。

请看下面示例:

  • playbook文件内容如下:

    [root@xuzhichao playbook]# cat status_ok.yml
    - hosts: localhost
    tasks:
    - name: Get Nginx Server Port
    shell: netstat -ntlp | grep nginx
    register: nginx_port - name: Print Nginx Port
    debug:
    msg:
    - "{{ nginx_port.stdout_lines }}"
  • 每次的执行结果“Get Nginx Server Port”任务状态都是changed:

    [root@xuzhichao playbook]# ansible-playbook status_ok.yml 
    
    PLAY [localhost] **********************************************************************************************************************************************
    
    TASK [Get Nginx Server Port] **********************************************************************************************************************************
    changed: [localhost] <==状态为changed TASK [Print Nginx Port] ***************************************************************************************************************************************
    ok: [localhost] => {
    "msg": [
    [
    "tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 26579/nginx: master "
    ]
    ]
    } PLAY RECAP ****************************************************************************************************************************************************
    localhost : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
  • 可以使用changed_when: false 来抑制这个状态,playbook修改如下:

    [root@xuzhichao playbook]# cat status_ok.yml
    - hosts: localhost
    tasks:
    - name: Get Nginx Server Port
    shell: netstat -ntlp | grep nginx
    register: nginx_port
    changed_when: false <==该task不再发生changed提示 - name: Print Nginx Port
    debug:
    msg:
    - "{{ nginx_port.stdout_lines }}"
  • 此时执行结果如下:

    [root@xuzhichao playbook]# ansible-playbook status_ok.yml 
    
    PLAY [localhost] **********************************************************************************************************************************************
    
    TASK [Get Nginx Server Port] **********************************************************************************************************************************
    ok: [localhost] <==变为OK状态 TASK [Print Nginx Port] ***************************************************************************************************************************************
    ok: [localhost] => {
    "msg": [
    [
    "tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 26579/nginx: master "
    ]
    ]
    } PLAY RECAP ****************************************************************************************************************************************************
    localhost : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

1.4 changed_when检查任务结果

仍然以安装httpd为例,在每次修改完httpd的配置文件后,都需要检查配置文件的语法是否正确,如果不正确,则退出后续task,不重启httpd服务。

  • playbook文件优化如下:

    [root@xuzhichao playbook]# cat change_when.yml
    ---
    - hosts: 192.168.20.23
    remote_user: root tasks:
    - name: Install Htttpd Server
    yum:
    name: httpd
    state: present
    tags:
    - install_httpd
    - install_apache - name: Configure Httpd Server
    copy:
    src: conf/httpd.conf.j2
    dest: /etc/httpd/conf/httpd.conf
    owner: "root"
    group: "root"
    mode: "0644"
    notify: Restart Httpd Server
    tags: conf_httpd - name: Check Httpd Configuer <==检测配置文件语法
    shell: /usr/sbin/httpd -t
    register: httpd_check
    changed_when:
    - httpd_check.stdout.find('OK') <==查找变量返回的结果是否有ok,如不存在则终止该tasks
    - false <==被管理主机没有发生变化状态则为OK - name: Init Httpd Server
    copy:
    src: file/test.html.j2
    dest: /var/www/html/test.html
    owner: "apache"
    group: "apache"
    mode: "0644"
    tags: init_httpd - name: Start Httpd Server
    service:
    name: httpd
    state: started
    enabled: yes
    tags: start_httpd handlers:
    - name: Restart Httpd Server
    service:
    name: httpd
    state: restarted
    tags: restart httpd [root@nginx03 ~]# httpd -t
    Syntax OK

    playbook运行结果如下:

    [root@xuzhichao playbook]# ansible-playbook change_when.yml 
    
    PLAY [192.168.20.23] ******************************************************************************************************************************************
    
    TASK [Install Htttpd Server] **********************************************************************************************************************************
    ok: [192.168.20.23] TASK [Configure Httpd Server] *********************************************************************************************************************************
    ok: [192.168.20.23] TASK [Check Httpd Configuer] **********************************************************************************************************************************
    ok: [192.168.20.23] TASK [Init Httpd Server] **************************************************************************************************************************************
    ok: [192.168.20.23] TASK [Start Httpd Server] *************************************************************************************************************************************
    changed: [192.168.20.23] PLAY RECAP ****************************************************************************************************************************************************
    192.168.20.23 : ok=5 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

ansible系列(28)--ansible的playbook异常处理的更多相关文章

  1. Ansible系列(五):playbook应用和roles自动化批量安装示例

    html { font-family: sans-serif } body { margin: 0 } article,aside,details,figcaption,figure,footer,h ...

  2. Ansible系列(三):YAML语法和playbook写法

    html { font-family: sans-serif } body { margin: 0 } article,aside,details,figcaption,figure,footer,h ...

  3. Ansible系列(四):playbook应用和roles自动化批量安装示例

    Ansible系列文章:http://www.cnblogs.com/f-ck-need-u/p/7576137.html playbook是ansible实现批量自动化最重要的手段.在其中可以使用变 ...

  4. Ansible系列(二):选项和常用模块

    html { font-family: sans-serif } body { margin: 0 } article,aside,details,figcaption,figure,footer,h ...

  5. Ansible系列(一):基本配置和使用

    本文目录:1.1 安装Ansible1.2 配置Ansible 1.2.1 环境配置 1.2.2 SSH互信配置 1.2.3 简单测试1.3 inventory Ansible是一种批量.自动部署工具 ...

  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. 循环 1.1 with_items迭代列表 1.2 with_dict迭代字典项 1.3 with_fileglob迭代文件 1.4 with_lines迭代行 1.5 with_ne ...

  8. Ansible系列(七):执行过程分析、异步模式和速度优化

    本文目录:1.1 ansible执行过程分析1.2 ansible并发和异步1.3 ansible的-t选项妙用1.4 优化ansible速度 1.4.1 设置ansible开启ssh长连接 1.4. ...

  9. ansible学习笔记三:playbook和roles

    参考博客: Ansible 系列之 Playbooks 剧本 -飞走不可(博客园) linux运维学习之ansible的playbook及roles的使用 - 51CTO博客 nginx 基于uwsg ...

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

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

随机推荐

  1. Java中关于优先队列PriorityQueue详解

    一.优先队列概述 优先队列PriorityQueue是Queue接口的实现,可以对其中元素进行排序, 可以放基本数据类型的包装类(如:Integer,Long等)或自定义的类 对于基本数据类型的包装器 ...

  2. 温馨提示:不注意这几点,PDT(产品开发团队)就得散!

    在IPD(集成产品开发)体系中,PDT(Product Development Team,产品开发团队)发挥着至关重要的作用.PDT是一个跨部门.跨职能的协作团队,其成员来自不同的专业领域,包括研发. ...

  3. 正则表达式环视匹配(?=pattern)、(?!pattern)、(?<=pattern)、(?<!pattern)怎么用

    今天在处理数据的时候遇到一个,需要用正则表达式匹配不包含某字符的字符串的问题,用到否定匹配,现总结如下: 一个正则小知识 ↓ []:表示范围,匹配其中任何一个 {}:表示重复匹配多次. ():表示分组 ...

  4. SqlServer的实用且高级玩法.md

    1.常见表表达式(CTEs) 如果您想要查询子查询,那就是CTEs施展身手的时候 - CTEs基本上创建了一个临时表. 使用常用表表达式(CTEs)是模块化和分解代码的好方法,与您将文章分解为几个段落 ...

  5. C++一些例子

    虚析构 #include<iostream> class Base { public: Base() { std::cout << "base 构造" &l ...

  6. #分治NTT#CF1218E Product Tuples

    Codeforces 用 OGF 表示 \(F(B,x)\) 就是 \[\prod_{i=1}^n(1+(q-a_i)x) \] 直接分治 NTT 把 \([x^k]\) 也就是这一位的系数求出来就可 ...

  7. #线段树分治,凸壳#洛谷 5416 [CTSC2016]时空旅行

    题目链接 题目大意 有 \(n\) 个平行宇宙,由某些平行宇宙添加或删除一个点(三维坐标)得到, 现在有 \(m\) 组询问,形如对于某个平行宇宙,费用为从该平行宇宙内某个点出发 到达指定 \(x\) ...

  8. [原创工具] 病毒整理器V0.98

    最近由于各种需要,所以开发了一个. RT,下载地址: 链接:https://pan.baidu.com/s/1Kxd77-n3fbPZQm_CIH6LTA 提取码:xq8f 很简单的一个小工具,以后还 ...

  9. 第八篇:socket网络编程

    一.网络编程简绍 二.socket连接过程 三.socket文件传输 四.socket循环接收 五.socket粘包处理 六.FTP文件传输 七.socketServer 八.web框架 #!/usr ...

  10. ImageJ使用教程(一):开始使用

    目录 简介 界面介绍 Edit->Options 开始使用 打开图片 放大拖拽 图片信息 色彩分析 保存图片 总结 参考文章 ImageJ软件 简介 ImageJ是一个基于java的公共的图像处 ...