How to Stop playbook execution based on a file ?

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?

Depending on the overall scenario you can use the fail module. Otherwise, you’ll need to put conditionals on all other tasks in the playbook to just skip when the condition is not met.