How to skip the playbook based on a condition

Hi,
I basically wanted to skip the remaining of the playbook if a file exists

  • name: Check if the file exists
    stat:
    path: /home/ansible/dbname/sql/file.sql
    register: optional_file

  • debug:
    msg: “File Exists”
    when: optional_file.stat.exists == True

Also, Is there a way I read the same file, and based on a string I stop the remaining playbook?

meta: end_play
is what you want

https://docs.ansible.com/ansible/latest/modules/meta_module.html

Thanks I got this working.