简体   繁体   English

Ansible 跳过无法访问的主机

[英]Ansible skip unreachable hosts

Hello I have written a script to ping all my inventory hosts.您好,我编写了一个脚本来 ping 我所有的库存主机。 Some are behind VPN services so before I can ping them I set up a tunnel.有些是在 VPN 服务之后,所以在我可以 ping 它们之前,我设置了一个隧道。

This works fine, however if the tunnel is set up but the ansible ping does not succeed the entire play just halts and none of the subsequent tasks get executed ( tunnel does not get closed / rest of tasks for host that are reachable do not get executed )这工作正常,但是,如果隧道已设置但 ansible ping 没有成功,则整个播放将停止并且后续任务都不会执行(隧道不会关闭/主机的其余任务不会被执行) )

How can I make the play continue and just skip the host that was unreachable?我怎样才能继续播放并跳过无法访问的主机? I've looked at "meta clear_host_errors" but that's not it.我看过“meta clear_host_errors”,但不是这样。

Here's my script这是我的脚本

- hosts:
    - liveservers-direct
    - liveservers-special
    - liveservers-keypair
    - testservers-direct
    - testservers-special
    - testservers-keypair
    - intern
  gather_facts: no
  strategy: debug
  become: no
  tasks:
  - name: Ping some servers
    ping:


- hosts:
    - liveservers-vpn
    - testservers-vpn
  strategy: debug
  gather_facts: no
  become: no
  serial: 1
  vars_files:
   - ../roles/vpn/vars/customers.yml
  tasks:
  - include: ../roles/vpn/tasks/connect.yml icao="{{hostvars[inventory_hostname]['icao']}}"
  - ping:
  - name:
    meta: clear_host_errors
  - include: ../roles/vpn/tasks/disconnect.yml icao="{{hostvars[inventory_hostname]['icao']}}"

fatal: [server.behind.vpn]: UNREACHABLE!致命:[server.behind.vpn]:无法访问! => {"changed": false, "msg": "Failed to connect to the host via ssh: ssh: connect to host 10.xx.xx.xx port 22: Connection timed out\\r\\n", "unreachable": true} => {"changed": false, "msg": "无法通过 ssh 连接到主机: ssh: 连接到主机 10.xx.xx.xx 端口 22: 连接超时\\r\\n", "无法访问" : 真的}

above error happens on "ping" how do I make it skip the failure and just continue with the rest of the hosts?上面的错误发生在“ping”上,我如何让它跳过失败并继续使用其余的主机? the play just stops now when it reaches the unreachable host, but a few more need to be checked当它到达无法访问的主机时,播放现在就停止了,但还有一些需要检查

This will soon be possible in the upcoming 2.7 release of Ansible, with the ignore_unreachable keyword. 在即将到来的2.7版本的Ansible中,使用ignore_unreachable关键字将很快可以实现这ignore_unreachable

See the release notes for 2.7 - https://github.com/ansible/ansible/blob/stable-2.7/changelogs/CHANGELOG-v2.7.rst#major-changes 请参阅2.7的发行说明-https: //github.com/ansible/ansible/blob/stable-2.7/changelogs/CHANGELOG-v2.7.rst#major-changes

New keyword ignore_unreachable for plays and blocks. 用于播放和阻止的新关键字ignore_unreachable Allows ignoring tasks that fail due to unreachable hosts, and check results with is unreachable test. 允许忽略由于主机不可达而失败的任务,并检查测试结果是否可达。

You could try using ignore_errors: yes . 您可以尝试使用ignore_errors: yes It will ignore any errors no matter what the reason is and it will continue with the rest of the tasks. 无论是什么原因,它将忽略任何错误,并将继续执行其余任务。

Ref: http://docs.ansible.com/ansible/latest/playbooks_error_handling.html 参考: http : //docs.ansible.com/ansible/latest/playbooks_error_handling.html

Removing serial keyword did fix the issue with the play being halted on an unreachable host. 删除串行关键字确实解决了在无法访问的主机上停止播放的问题。 However my VPN connection play isn't written with parallel processing in mind and so I'll have to change that. 但是,我的VPN连接过程在编写时并未考虑并行处理,因此我必须进行更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM