ansible模块wait_for等待端口打开卡住

时间:2022-05-18 00:37:42

一、相关yaml

---
- name: "开始进行环境发布"
hosts: all
vars:
- local_jar_path: "{{ local_path }}/{{ module_jar_name }}"
- remote_jar_path: "{{ module_path }}/bin"
gather_facts: no
tasks:
- name: "关闭jar服务"
shell: "{{ remote_jar_path }}/stop.sh &"

- name: "检测jar服务端口直到关闭"
wait_for:
port: "{{ module_port }}"
delay: 2
state: stopped

- name: "清理jar日志"
shell: "rm -rf {{ remote_jar_path }}/../logs/* &>/dev/null warn=false"
#when: cleanlog == true

- name: "拷贝本地war包到远程路径"
copy:
src: "{{ local_jar_path }}"
dest: "{{ remote_jar_path }}"

- name: "启动jar服务"
shell: "nohup {{ remote_jar_path }}/start.sh &"

- name: "检测jar服务端口直到打开"
wait_for:
port: "{{ module_port }}"
delay: 10
state: started

二、问题

1、问题:
运行的时候可以成功关闭服务,检测到端口关闭,启动服务,然后检测服务端口打开的时候卡主,直到超时报错
"fatal: [192.168.10.131]: FAILED! => {"changed": false, "elapsed": 300, "msg": "Timeout when waiting for 127.0.0.1:66666"}"

2、业务程序那边服务监听的具体ip,没开启全网监听0.0.0.0

3、查看host备注
ansible-doc wait_for
- host
A resolvable hostname or IP address to wait for.
[Default: 127.0.0.1]

4、修改yaml
- name: "检测jar服务端口直到打开"
wait_for:
port: "{{ module_port }}"
host: "{{ inventory_hostname }}" #此处将host改成ansible中hosts配置的业务ip地址
delay: 10
state: started

5、重新启动正常运行