how to use Ansible meta module to halt playbook execution upon task Failure.

14 views
Skip to first unread message

Mohtashim S

unread,
Aug 13, 2019, 4:27:20 PM8/13/19
to Ansible Project
I wish to search for a string ("AC245") in all files with extension *.db under /home/examples directory.

If the string is found I wish to abort the play immediately marking the playbook as FAILED. 

Apart from aborting the play using "meta: end_play" I was able to achieve everything. 

Can you help suggest how can I update my current code to add the end_play feature as soon as the string is found in any *.db file ?

---
- name: "Find the details here "


  hosts
: localhost
  any_errors_fatal
: true
  serial
: 1
  tasks
:
  
- name: Ansible find files multiple patterns examples
    find
:
      paths
: /home/examples
      patterns
: "*.db"
      recurse
: yes
    
register: files_matched


  
- name: Search for Number in the matched files
    command
: grep -{{ myString }} {{ item.path }}
    
register: command_result
    failed_when
: command_result.rc == 0
    with_items
:
       
- "{{ files_matched.files }}"  

You can run the above find.yml using this command: ansible-playbook find.yml -e "myString=AC245"

Zolvaring

unread,
Aug 16, 2019, 1:51:59 AM8/16/19
to Ansible Project
If you want to use meta it's simple but will not mark the play as failed, as far as I know there's no explicit way to do that with meta:

- name: abort if string was found
meta: end_play
when: command is failed


It might be better to try the fail module since it will return the failed state you want:

- name: fail if string was found
fail:
msg: string was found!
when: command is failed

Reply all
Reply to author
Forward
0 new messages