zoukankan      html  css  js  c++  java
  • ansible中的重试逻辑

    背景

    经常出现访问某个git,或者 url的时候,会出现 失败,需要 重试逻辑

    一般是通过: retries, delay, when, until 几个语句联动完成的。

    核心是调试方法; 通过debug: var=xxx , 读取变量的数据样式,然后,写when条件和until条件。

    方案

    - name: "[BASIC] [05_disk] handle disk mount logic for all spec"
      tags: mre-disk
      block: <<==0. 先搞一个block,在一个block里,所有register的变量作用域
        - name: "[BASIC] [05_disk] fetch machine spec from cmdb api"
          uri:
            url: "{{ devops_api.host }}/xxxxxxxxx"
            method: POST
            body: { "ip_query_condition": {"ip_scope": "bk_host_innerip", "exact_match": "1", "ip_list": ["{{ ansible_host }}"]} }
            body_format: json
          register: host_search  <<== 1. 将url的结果存到变量: host_search
    
        - debug: var= host_search  <<== 2. 打印host_search 的结果,做调试, 后面才知道 应该如何 写判断条件
    
        - name: "[BASIC] [05_disk] check if the server is [H14 Server]"
          shell: "echo This is H14"
          register: machine_spec
          when:
            - host_search.json.data.info[0].host.spec == "H14"
    
        - debug: var=machine_spec
    
        - name: "[BASIC] [05_disk] copy auto-init script"
          copy:
            src: files/init_disk_for_spec.sh
            dest: /tmp/init_disk_for_spec.sh
            owner: root
            group: root
            mode: '0644'
    
        - name: "[BASIC] [05_disk] init disk [H14 Server]"
          shell: "bash /tmp/init_disk_for_spec.sh H14"
          register: H14_output
          until: H14_output.rc == 0 <<== 4.你需要先知道H14_output 是啥? 可以通过- debug: var=H14_output 来看
          retries: 3
          delay: 3  <<== 重试间隔3s
          when: host_search.json.data.info[0].host.spec == "H14" <<== 3.根据变量写判断条件,python风格
    
        - debug: var=H14_output
        - debug: var=H14_output.rc
    ```
    
    测试方法: 
    
    ```
    ansible-playbook -i inventory/ahao.mu.D2_V2.ip playbooks/infra_init_new.yml --tags slow_jobs
    ```
    muahao@aliyun.com
  • 相关阅读:
    转: wireshark过滤规则
    PHP开发
    转:python安装pycrypto
    How to use pycharm to debug scrapy projects
    VCForPython27.msi安装后, 还显示error: Unable to find vcvarsall.bat
    Dcgpofix
    Dsamain
    组托管服务帐户概述
    创建 PSO
    介绍 Active Directory 域服务 (AD DS) 虚拟化
  • 原文地址:https://www.cnblogs.com/muahao/p/15239468.html
Copyright © 2011-2022 走看看