ansible - 当主机无法访问时,ansible跳过失败模块
问题描述
当某些主机无法访问时,我正在尝试打印自定义消息。我的问题是,当主机不可达时,它将在下一个任务中被跳过,因此永远不会触发失败模块。并且可访问的主机也将由于when condition are not met
.
我试过ignore_unreachable: false
了,还是一样。任何想法将不胜感激。
---
- hosts: prod
gather_facts: no
tasks:
- name: fail when not reachable
action: ping
register: ping_result
# any_errors_fatal: true
- fail:
msg: "please make sure all server up and run again"
when: ping_result.unreachable is defined
any_errors_fatal: true
解决方案
您正在寻找ignore_unreachable
应设置为 的关键字 ,true
因为您确实想忽略无法访问的主机以继续执行您的fail
任务。
鉴于剧本:
- hosts: all
gather_facts: no
tasks:
- ping:
register: ping_result
ignore_unreachable: true
- fail:
msg: "please make sure all server up and run again"
when: ping_result.unreachable is defined
any_errors_fatal: true
这产生:
PLAY [all] **********************************************************************************************************
TASK [ping] *********************************************************************************************************
fatal: [node1]: UNREACHABLE! => {"changed": false, "msg": "[Errno -2] Name does not resolve", "skip_reason": "Host node1 is unreachable", "unreachable": true}
fatal: [node2]: UNREACHABLE! => {"changed": false, "msg": "[Errno -2] Name does not resolve", "skip_reason": "Host node2 is unreachable", "unreachable": true}
fatal: [node3]: UNREACHABLE! => {"changed": false, "msg": "[Errno -2] Name does not resolve", "skip_reason": "Host node3 is unreachable", "unreachable": true}
ok: [localhost]
TASK [fail] *********************************************************************************************************
skipping: [localhost]
fatal: [node1]: FAILED! => {"changed": false, "msg": "please make sure all server up and run again"}
fatal: [node2]: FAILED! => {"changed": false, "msg": "please make sure all server up and run again"}
fatal: [node3]: FAILED! => {"changed": false, "msg": "please make sure all server up and run again"}
NO MORE HOSTS LEFT **************************************************************************************************
PLAY RECAP **********************************************************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
node1 : ok=0 changed=0 unreachable=1 failed=1 skipped=1 rescued=0 ignored=0
node2 : ok=0 changed=0 unreachable=1 failed=1 skipped=1 rescued=0 ignored=0
node3 : ok=0 changed=0 unreachable=1 failed=1 skipped=1 rescued=0 ignored=0
推荐阅读
- node.js - 哪些请求参数可用于判断请求是否来自 Postman 与 NodeJS 服务器?
- html - 更新图形布局 d3 库的问题
- python - 原始错误是:dlopen(/Users/ulto4/miniforge3/envs/python386/lib/python3.8/site-packages/numpy/core/_multiarray_umath.cpython-38-darwin.so, 2):
- javascript - 横幅转换器不工作。怎么解决?
- r - 从 rmd 针织文件中渲染的 pdf 在 Rstudio 和 Ubuntu 中未显示绘图标签
- javascript - 在 Jest for Node 应用程序中模拟环境和/或导出的常量
- javascript - Javascript,点击时需要返回到某个位置
- c - 将for循环程序修改为while循环程序的问题
- c# - 何时将样式内的 ControlTemplate 定义为资源?
- javascript - 如何用 Jest 测试这个事件