This is perhaps an unorthodox way to check if a file contains specific text, but it does seem to be the simplest and mostly commonly recommended on Ansible forums.
- name: "Search for mytext in /path/to/file_to_check" lineinfile: path: /path/to/file_to_check line: "mytext" state: present check_mode: yes register: myTextCheck
The key thing to note here is the check_mode which effectively performs a dry run for lineinfile without actually modifying the file. As such, if the myTextCheck variable is ‘changed’, then this means that the file does not contain mytext.
- name: Task to run if mytext DOES NOT EXIST in file_to_check shell: /dir/script_to_run.sh register: anyVar ignore_errors: true when: 'myTextCheck is changed'