Ansible doesn't abort even after a command fails executing
I am using ansible to execute commands by picking up each one from a text file (say sample.txt)
sample.txt has following commands
echo "hello world"
df -h
free -m
mkdir /tmp/`hostname`_bkp
touch /tmp/`hostname`_bkp/file{1..5}
mvn -version
echo "directory and files craeted"
echo "Bye.!"
Now if start running playbook it executes but never aborts when command fails, it just shows error and executes next command.
I want to abort the ansible when command fails like in my case I dont have maven installed so "mvn -version" should fail and ansible should abort.
I've written my playbook as follows
---
- hosts: localhost
tasks:
- name: this command prints FAILED when it fails
command: cat /root/ansi-pb/sample.txt
register: my_items
failed_when: "'command not found' in my_items.stderr"
- name: installation
shell: "{{ item }}"
with_items: "{{ my_items.stdout_lines }}"
# failed_when: "'command not found' in my_items.stderr"
# fail: msg="command failed"
# when: "'command not found' in my_items.stderr"
# - name: fail the play if the previous command did not succeed
# fail: msg="the command failed"
# when: "'command not found' in my_items.stderr"
# - fail:
# msg: "The system may not be provisioned according to the CMDB status."
# when: "'command not found' in my_items.stderr"
and also I want ansible to start execution from where it failed like if it fails at "mvn -version" then next time when I run playbook it should start from that command instead of starting from first command in the text file.
How can i achieve this?
|