I'm looking for a way to run a task on host A to be completed before running another task on host B.
---
- hosts: A
tasks:
- name: Do stuff
- hosts: B
tasks: # <= Run this once the "Do stuff" task is over and successful
- name: Do other stuff
Until now I've been using two playbooks and run the one with A first. What's the best way to achieve this?
The default behavior in ansible is actually that tasks within a playbook are run consecutively rather than in parallel.
However, ansible will by default progress with subsequent tasks even after encountering a failure. You can avoid the second task being executed by forcing errors to be treated as fatal, like this:
---
- hosts: A
tasks:
- name: Do stuff
any_errors_fatal: true
- hosts: B
tasks: # <= Run this once the "Do stuff" task is over and successful
- name: Do other stuff
Not sure if I understood correctly but maybe this approach will help you:
---
- hosts: A
tasks:
- name: Spin up a VPS
# Do your stuff and some kind of verification e.g
shell: '<insert command here>'
register: host_uptime
- hosts: B
tasks: # <= Run this once theverification task is over and successful
- name: Install curl
package:
name: "curl"
state: present
when: "{{ host_uptime.rc }}" == 0
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With