Jim
Jim

Reputation: 14280

Can't tell when Ansible server is up

I have an Ansible playbook for creating Linode servers. The problem I'm having is that my playbook isn't able to determine when the new server is up. I'm using Ansible 2.8.4. My playbook is as follows:

---
hosts: 127.0.0.1
gather_facts: False

- name: create server
  linode_v4:
    label: "{{ host_name }}_{{ 100 | random }}"
    access_token: "{{ linode_api4_key }}"
    type: "{{ plan_1GB }}"
    region: "{{ region_us_central }}"
    image: "{{ image_debian_10 }}"
    root_pass: "{{ linode_root_password }}"
    authorized_keys: "{{ my_ssh_public_key }}"
    tags: "inventory.ini"
    state: present
  register: linode

- name: save new server's ip address to a fact
  set_fact: ip_addr={{ linode.instance.ipv4 }}
  tags: always

- debug:
    var: ip_addr

- name: wait until new server is up and listening on port 22
  wait_for:
    host: "{{ ip_addr }}"
    port: 22
    delay: 2
    timeout: 600
    state: started
    msg: "Server port is not listening"
  tags: always

I also tried it this way:

- name: wait until new server is up
  local_action:
    module: wait_for
    state: started
    host: "{{ ip_addr }}"
    port: 22
    delay: 1
    timeout: 100

I've tried doing it using a wait_for and also via local_action but neither one is working. The playbook never returns from the wait for task. I monitor my Linode dashboard as the playbook runs and I can see that that IP address I'm feeding to the task via "ip_addr" is correct and the dashboard also shows me when the server is up. What am I doing wrong?

Upvotes: 1

Views: 178

Answers (2)

Veridian
Veridian

Reputation: 11

You might try this.

- name: Check if host is accessible
  wait_for: host=<host to check> port=80 timeout=3
  register: host_accessible
  failed_when: False

- include: next_task_you_want.yml
  when: host_accessible.state is defined and host_accessible.state == "started"

Upvotes: 1

user7379804
user7379804

Reputation: 23

I have solved a similar problem with timeouts by registering the result of the action and retrying until the result isn't failed. You can configure the delay between retries and the maximum number of retries:

Also, I noticed you werent including state in your play, that might be causing an issue.

- name: wait until new server is up and listening on port 22
  wait_for:
    host: mywebserver.com
    port: 22
    state: started         # Port should be open
    delay: 0               # No wait before first check (sec)
    timeout: 4             # Stop checking after timeout (sec)
  register: waitfor_result
  until: waitfor_result is not failed
  retries: 50
  delay: 10

Upvotes: 1

Related Questions