Wait for Port to Close Using Ansible


Sometimes it is useful to wait for a port to be closed, for example when updating an app that can’t always properly be shut down using other Ansible modules.

This can easily be achieved using the ansible.builtin.wait_for or ansible.builtin.win_wait_for module.

- name: Wait for port to close
  hosts: localhost
  tasks:
    - name: Wait for port 8000 close on the host, fail after 100 seconds
      ansible.builtin.wait_for:
        port: 8000
        state: stopped
        timeout: 100

    - name: Display the config
      ansible.builtin.debug:
        msg: "Port is now closed"

See also