Hello I have written a script to ping all my inventory hosts. Some are behind VPN services so before I can ping them I set up a tunnel.
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 )
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.
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! => {"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}
above error happens on "ping" how do I make it skip the failure and just continue with the rest of the hosts? 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.
See the release notes for 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. Allows ignoring tasks that fail due to unreachable hosts, and check results with is unreachable test.
You could try using 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
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.