无法判断 Ansible 服务器何时启动



我有一个用于创建Linode服务器的Ansible剧本。 我遇到的问题是我的剧本无法确定新服务器何时启动。 我正在使用 Ansible 2.8.4。 我的剧本如下:

---
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

我也尝试过这种方式:

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

我尝试使用wait_for和local_action来做到这一点,但两者都不起作用。 剧本永远不会从等待任务中返回。 当剧本运行时,我监视我的 Linode 仪表板,我可以看到我通过"ip_addr"提供给任务的 IP 地址是正确的,并且仪表板还会在服务器启动时显示我。 我做错了什么?

我通过注册操作结果并重试直到结果不失败来解决类似的超时问题。您可以配置重试之间的延迟和最大重试次数:

另外,我注意到您在游戏中没有包含状态,这可能会导致问题。

- 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

你可以试试这个。

- 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"

最新更新