ImranRazaKhan
ImranRazaKhan

Reputation: 2297

Ansible Playbook should stop execution if port is open

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."

Upvotes: 1

Views: 2527

Answers (1)

ilias-sp
ilias-sp

Reputation: 6685

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

Upvotes: 1

Related Questions