背景
经常出现访问某个git,或者 url的时候,会出现 失败,需要 重试逻辑
方案
- 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
```