我打算等待一个端口准备好如此使用" wait_for"
- name: Wait for service to be ready
wait_for:
host: 192.168.1.70
port: 8080
然而它只等待300秒然后失败。
TASK [Wait for service to be ready]
fatal: [localhost]: FAILED! => {"changed": false, "elapsed": 300, "msg": "Timeout when waiting for 192.168.1.70:8080"}
所以,我添加了一个"直到" :
- name: Wait for service to be ready
wait_for:
host: 192.168.1.70
port: 8080
register: port8080
until: port8080.failed == "false"
相反,我收到错误:
TASK [Wait for ssh to be ready]
fatal: [localhost]: FAILED! => {"msg": "The conditional check 'port8080.failed == \"false\"' failed. The error was: error while evaluating conditional (port8080.failed == \"false\"): 'dict object' has no attribute 'failed'"}
但如果我删除"直到"并添加一个调试语句,我可以看到 port8080.failed 按预期设置为true或false。
TASK [debug] ****************************************************************************************************************************
ok: [localhost] => {
"port8080": {
"changed": false,
"elapsed": 0,
"failed": false,
第一个问题:这是一个安全的错误还是我做错了什么? 第二个问题:如何让我的脚本等待连接(超过300秒)?
(NB我实际上正在等待数据库启动,数据库启动300秒的时间太短。但我不想等待300秒,以防万一。 - 运行脚本并且DB已经在运行。)
答案 0 :(得分:3)
尝试wait_for_connection例如,当您重新启动系统并希望等到可达/可用时,这将起作用:
- name: Something that may reboot
shell: echo "true"
register: should_reboot
- name: Rebooting ...
shell: sleep 2 && /sbin/shutdown -r now "Reboot required"
async: 1
poll: 0
ignore_errors: true
register: rebooting
when: should_reboot.stdout == "true"
- name: Wait for rebooted servers to come back
wait_for_connection:
connect_timeout: 20
sleep: 5
delay: 5
timeout: 60
when: rebooting|changed
如果检查/测试远程主机是否已启动,则可能有效:
- name: Wait for service to be ready
wait_for:
port: 8080
host: 127.0.0.1
connect_timeout: 3
delay: 3
timeout: 30
为了测试,我使用netcat创建了一个本地端口8080,例如:
nc -l 8080
如果端口没有打开,你会得到类似的东西:
fatal: [localhost]: FAILED! => {"changed": false, "elapsed": 30, "msg": "Timeout when waiting for localhost:8080"}
当端口打开时你运行playybook会注意到连接它的人也会退出,如果你想保持它永久使用的话:
nc -kl 8080