如果端口打开,Ansible Playbook应停止执行

如果端口打开,Ansible Playbook应停止执行,ansible,ansible-2.x,Ansible,Ansible 2.x,我正在VM上安装karaf,在我的安装过程中,我想验证karaf实例是否已经在运行,如果端口打开,它将停止安装过程并退出ansible执行。目前它正在以另一种方式进行 - name: Wait for my-service to start wait_for: port={{karaf_port}} timeout=30 msg: "Port 8101 is not accessible." 您可以使用此任务检查应用程序是否已在运行。如果正在运行,它将中止剧本 -

我正在VM上安装karaf,在我的安装过程中,我想验证karaf实例是否已经在运行,如果端口打开,它将停止安装过程并退出ansible执行。目前它正在以另一种方式进行

- 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
基本上,您使用的模块状态为:stopped,ansible希望端口以某种方式停止侦听
超时
秒。如果端口保持运行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
子句,它只是为我的测试添加的


希望它能有所帮助

,但是如果您已经安装了应用程序,但应用程序没有运行呢?如果您通过另一种方式(如某些目录存在或不存在)进行检查,我将更有意义。我正在更改现有的shell脚本,因此现在我必须验证端口。了解,请参阅下面可能的实现