如果端口打开,Ansible Playbook应停止执行(Ansible Playbook should stop execution if port is open)

编程入门 行业动态 更新时间:2024-10-24 14:22:26
如果端口打开,Ansible Playbook应停止执行(Ansible Playbook should stop execution if port is open)

我正在虚拟机上安装karaf,并且在我的安装过程中,我想验证karaf实例是否已经在运行,并且在端口打开时停止安装过程并退出正确的执行。 目前它以其他方式

- name: Wait for my-service to start wait_for: port={{karaf_port}} timeout=30 msg: "Port 8101 is not accessible."

I am installing karaf on VM and in my installation process i want to verify if karaf instance is already running and it stop installation process and exit from ansible execution if port is open. currently its doing other way

- name: Wait for my-service to start wait_for: port={{karaf_port}} timeout=30 msg: "Port 8101 is not accessible."

最满意答案

您可以使用此任务来检查应用程序是否已在运行。 如果运行,它会中止剧本。

- 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的模块,并且有理由期望端口以某种方式停止监听timeout秒数。 如果端口保持10秒钟(它会保持不变,因为没有人停止已经安装的应用程序),否则会导致错误退出。

改变端口为23,你会看到剧本将继续下一步:

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条款,只是为我的测试添加。

希望能帮助到你

You could use this task to check if the application is already running. If running, it will abort the playbook.

- 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

Basically, you are using the module with state: stopped, and ansible expects the port to somehow stop listening for timeoutseconds. if port stays up for 10 seconds (it will stay up since nobody stops the already installed application), ansible will exit with error.

change port to 23, you will see playbook would continue to next step:

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

you dont need the register clause, just added for my test.

hope it helps

更多推荐

本文发布于:2023-08-07 13:24:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464218.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:端口   Playbook   Ansible   stop   port

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!