我正在使用 ansible 一次针对多个服务器运行命令。我想忽略任何因为“"SSH Error: data could not be sent to remote host \"1.2.3.4\". Make sure this host can be reached over ssh"
”而失败的主机' 错误,因为列表中的某些主机将脱机。我怎样才能做到这一点? ansible 中是否有默认选项可以忽略离线主机而不会使剧本失败?是否可以选择在单个 ansible
中执行此操作剧本之外的 cli 参数?
更新:我知道 ignore_unreachable: true
适用于 ansible 2.7 或更高版本,但我在 ansible 2.6.1 环境中工作。
最佳答案
我找到了一个很好的解决方案 here .您在本地 ping 每个主机以查看是否可以连接,然后针对通过的主机运行命令:
---
- hosts: all
connection: local
gather_facts: no
tasks:
- block:
- name: determine hosts that are up
wait_for_connection:
timeout: 5
vars:
ansible_connection: ssh
- name: add devices with connectivity to the "running_hosts" group
group_by:
key: "running_hosts"
rescue:
- debug: msg="cannot connect to {{inventory_hostname}}"
- hosts: running_hosts
gather_facts: no
tasks:
- command: date
关于ssh - Ansible 如何在 ansible 2.7.x 之前忽略无法访问的主机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55188936/