让ansible等待服务器启动,无需登录
Make ansible wait for server to start, without logging in
当我配置新服务器时,从我创建它到它可用之间存在延迟。所以我需要等到它准备好。
我认为那是 wait_for
任务的目的:
主机:
[servers]
42.42.42.42
playbook.yml:
---
- hosts: all
gather_facts: no
tasks:
- name: wait until server is up
wait_for: port=22
失败 Permission denied
。我想那是因为还没有任何设置。
我预计 它会打开一个 ssh 连接并等待提示 - 只是为了查看服务器是否已启动。但是 实际发生的事情 是它尝试登录。
是否有其他方法可以执行不尝试登录的等待?
如您正确所述,此任务在 "to be provisioned" 主机上执行,因此 ansible 首先尝试连接到它(通过 ssh),然后尝试等待端口启动。这适用于其他 ports/services,但不适用于给定主机上的 22,因为 22 是用于在该主机上执行任何任务的 "prerequisite"。
你可以做的是尝试 delegate_to
这个任务到 ansible 主机(你 运行 PB)并在 wait_for
中添加 host
参数任务。
示例:
- name: wait until server is up
wait_for:
port: 22
host: <the IP of the host you are trying to provision>
delegate_to: localhost
希望对您有所帮助
Q: "Is there some other way to perform a wait that doesn't try to login?"
答:可以wait_for_connection。例如
- hosts: all
gather_facts: no
tasks:
- name: wait until server is up
wait_for_connection:
delay: 60
timeout: 300
当我配置新服务器时,从我创建它到它可用之间存在延迟。所以我需要等到它准备好。
我认为那是 wait_for
任务的目的:
主机:
[servers]
42.42.42.42
playbook.yml:
---
- hosts: all
gather_facts: no
tasks:
- name: wait until server is up
wait_for: port=22
失败 Permission denied
。我想那是因为还没有任何设置。
我预计 它会打开一个 ssh 连接并等待提示 - 只是为了查看服务器是否已启动。但是 实际发生的事情 是它尝试登录。
是否有其他方法可以执行不尝试登录的等待?
如您正确所述,此任务在 "to be provisioned" 主机上执行,因此 ansible 首先尝试连接到它(通过 ssh),然后尝试等待端口启动。这适用于其他 ports/services,但不适用于给定主机上的 22,因为 22 是用于在该主机上执行任何任务的 "prerequisite"。
你可以做的是尝试 delegate_to
这个任务到 ansible 主机(你 运行 PB)并在 wait_for
中添加 host
参数任务。
示例:
- name: wait until server is up
wait_for:
port: 22
host: <the IP of the host you are trying to provision>
delegate_to: localhost
希望对您有所帮助
Q: "Is there some other way to perform a wait that doesn't try to login?"
答:可以wait_for_connection。例如
- hosts: all
gather_facts: no
tasks:
- name: wait until server is up
wait_for_connection:
delay: 60
timeout: 300