How to execute Play 2 only if Play 1 succeeds

22 views
Skip to first unread message

Shifa Shaikh

unread,
Mar 29, 2020, 6:59:59 AM3/29/20
to Ansible Project

I have two Plays having one task each.


The first Play 1 checks if the/var/test.datexists on each target host. 


Only if the Play 1 is successful do I want the second play Play 2 to run which executes scripts in parallel.


If Play 1 fails i.e if the test.dat does not exist I wish to terminate the entire playbook without the Play 2 getting executed. 


For this purpose, I have set any_errors_fatal set to true


I need to have an ansible Play strategy set to free as each of the scripts takes 30 minutes to complete hence running them in parallel is the requirement.


My understanding of ansible is limited.


I understand that if I have both the tasks under a single Play and set the strategy to free both the tasks will run in parallel which is something I do not want.


---

- name: Play 1- check for login and script
  hosts
: all_hosts
  any_errors_fatal
: true
  strategy
: free
  tasks
:

   
- name: Check script existence
     shell
: "ls /var/test.dat"
     
register: checkscript

   
- name:
     fail
:
       msg
: "script {{ scriptdet }} missing on {{ inventory_hostname }}"
     
when: checkscript.rc != 0

- name: Play 2- Run scripts
  hosts
: all_hosts
  user
: "{{ USER }}"
  strategy
: free

  tasks
:
   
- name: Execute backup script
     shell
: "{{ scriptdet }}"
     args
:
       chdir
: ~/..


I tried the above playbook but I see the second play executes despite the first play's task failed.


Can you please suggest how can I get this to work?

Dick Visser

unread,
Mar 29, 2020, 10:38:52 AM3/29/20
to ansible...@googlegroups.com
It works if you do it in a single play. 
Why are you using two different plays anyway?




--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/1b0ccbc8-6dac-4d90-acd4-c4a852b38f5b%40googlegroups.com.
--
Sent from a mobile device - please excuse the brevity, spelling and punctuation.
Reply all
Reply to author
Forward
0 new messages