lonix
lonix

Reputation: 20591

Make ansible wait for server to start, without logging in

When I provision a new server, there is a lag between the time I create it and it becomes available. So I need to wait until it's ready.

I assumed that was the purpose of the wait_for task:

hosts:

[servers]
42.42.42.42

playbook.yml:

---
- hosts: all
  gather_facts: no
  tasks:
  - name: wait until server is up
    wait_for: port=22

This fails with Permission denied. I assume that's because nothing is setup yet.

I expected it to open an ssh connection and wait for the prompt - just to see if the server is up. But what actually happens is it tries to login.

Is there some other way to perform a wait that doesn't try to login?

Upvotes: 1

Views: 2633

Answers (2)

Vladimir Botka
Vladimir Botka

Reputation: 68034

Q: "Is there some other way to perform a wait that doesn't try to login?"

A: It is possible to wait_for_connection. For example

- hosts: all
  gather_facts: no
  tasks:
    - name: wait until server is up
      wait_for_connection:
        delay: 60
        timeout: 300

Upvotes: 1

ilias-sp
ilias-sp

Reputation: 6685

as you correctly stated, this task executes on the "to be provisioned" host, so ansible tries to connect to it (via ssh) first, then would try to wait for the port to be up. this would work for other ports/services, but not for 22 on a given host, since 22 is a "prerequisite" for executing any task on that host.

what you could do is try to delegate_to this task to the ansible host (that you run the PB) and add the host parameter in the wait_for task.

Example:

- name: wait until server is up
  wait_for: 
    port: 22
    host: <the IP of the host you are trying to provision>
  delegate_to: localhost

hope it helps

Upvotes: 3

Related Questions