我正在VM上安装karaf,在我的安装过程中,我想验证karaf实例是否已经在运行,并且如果端口打开则停止安装过程并退出ansible执行。目前正采取其他方式
- name: Wait for my-service to start
wait_for: port={{karaf_port}} timeout=30
msg: "Port 8101 is not accessible."
答案 0 :(得分:1)
您可以使用此任务来检查应用程序是否已在运行。如果正在运行,它将中止剧本。
- name: Check if service is running by querying the application port
wait_for:
port: 22
timeout: 10
state: stopped
msg: "Port 22 is accessible, application is already installed and running"
register: service_status
基本上,您正在使用带有state: stopped
的模块,并且ansible期望端口以某种方式停止侦听timeout
秒。如果端口保持10秒钟(由于没有人停止已安装的应用程序,它将保持运行状态),ansible将退出并显示错误。
将端口更改为23,您将看到playbook将继续下一步:
tasks:
- name: Check if service is running by querying the application port
wait_for:
port: 23
timeout: 10
state: stopped
msg: "Port 23 is accessible, application is already installed and running"
register: service_status
- name: print
debug:
var: service_status
你不需要register
条款,只是为我的测试添加了。
希望有所帮助