ssh - Ansible 如何在 ansible 2.7.x 之前忽略无法访问的主机

标签 ssh ansible

我正在使用 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/

相关文章:

git - 使用 JGit 的 SSH 身份验证安全地访问 Git 存储库/身份验证失败

linux - 在尝试使用 ssh 设置时间时寻找匹配的 `"时出现意外的 EOF

linux - Gitosis 不接受主机名中的数字

安塞 bool 错误! 'retries' 不是任务包含的有效属性

bash - 在非交互式Shell session 中运行多个命令并解析输出

python-2.7 - SSH 窗口大小如何影响 paramiko

pip - 开发使用 3rd 方 python 库的自定义 ansible 模块时设置环境的正确方法

ansible - 如何使用ansible安装fail2ban?

docker - 如何在Docker容器中运行Ansible脚本

yaml - 我的 Ansible 语法有什么愚蠢之处?当我调用 list_item 时,我被告知它不存在