简体   繁体   中英

ansible reconnect after service restart

I have a handler to restart service on config changes:

- name: restart openvpn
  service: name=openvpnas state=restarted

However ansible can't handle it properly:

fatal: [vpn]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: ssh: connect to host 10.10.10.2 port 22: Connection refused", "unreachable": true}

Ansible can work with host reboots but how do I deal with this kind of service restarts?

As a temporary workaround:

- name: restart openvpn
  debug: msg="restarting openvpn"
  changed_when: yes
  notify:
      - restart openvpn raw
      - reconnect

- name: restart openvpn raw
  service: name=openvpnas state=restarted
  ignore_errors: yes
  async: 300
  poll: 0

- name: reconnect
  wait_for_connection:
    delay: 5
    timeout: 300

However service restart task still fails even in async mode.

As of Ansible 2.7, ignore_unreachable can be used. I do think it is a slightly cleaner solution as it only ignores connection errors.

I use a handler like this:

- name: restart Tor
  service:
    name: tor@default
    state: restarted
  ignore_unreachable: yes

- name: reconnect
  wait_for_connection:
    connect_timeout: 15
    sleep: 5 
    timeout: 300
  listen: restart Tor

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.

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