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

Ale*_*hen 7 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 环境中工作。

Ale*_*hen 14

我在这里找到了一个很好的解决方案。您在本地 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
Run Code Online (Sandbox Code Playgroud)


sor*_*rin 7

使用 Ansible (2.8) 上的当前版本,这样的事情是可能的:

- name: identify reachable hosts
  hosts: all
  gather_facts: false
  ignore_errors: true
  ignore_unreachable: true
  tasks:
    - block:
        - name: this does nothing
          shell: exit 1
          register: result
      always:
        - add_host:
            name: "{{ inventory_hostname }}"
            group: reachable

- name: Converge
  hosts: reachable
  gather_facts: false
  tasks:
    - debug: msg="{{ inventory_hostname }} is reachable"
Run Code Online (Sandbox Code Playgroud)