LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Ansible doesn't abort even after a command fails executing (https://www.linuxquestions.org/questions/linux-newbie-8/ansible-doesnt-abort-even-after-a-command-fails-executing-4175600746/)

hruday 02-28-2017 02:58 AM

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?

mkempster22 03-01-2017 09:52 AM

Have you tried the option stated here:
http://docs.ansible.com/ansible/play..._handling.html

Under 'Aborting the Play'

Could be too extreme for what you're looking for


All times are GMT -5. The time now is 10:55 AM.