Ansible execute task on different hosts in order
Q: &quot;Is it possible to use Ansible to manage the order of programs?&quot;
A: Yes. It's possible. For example, the playbook below does the job
- hosts: HostA
  tasks:
    - include_role:
        name: startprogram

- hosts: HostB
  tasks:
    - include_role:
        name: runcommand
      when: hostvars.HostA.HostAprogram_started|default(false)

- hosts: HostC
  tasks:
    - include_role:
        name: startsecprogram
      when: hostvars.HostB.HostBcommand_passed|default(false)

- hosts: HostD,HostE
  tasks:
    - debug:
        msg: command on {{ inventory_hostname }} which requires HostC program to run
      when: hostvars.HostC.HostCprogram_started|default(false)


Example of the roles for the purpose of testing
shell&gt; cat roles/startprogram/tasks/main.yml
- debug:
    msg: /etc/init.d/HostAprogram.sh start
- set_fact:
    HostAprogram_started: true

shell&gt; cat roles/runcommand/tasks/main.yml
- debug:
    msg: command which using host A
- set_fact:
    HostBcommand_passed: true

shell&gt; cat roles/startsecprogram/tasks/main.yml
- debug:
    msg: /etc/init.d/HostCprogram.sh start
- set_fact:
    HostCprogram_started: true

Example of the output
ok: [HostA] =&gt; {
    &quot;msg&quot;: &quot;/etc/init.d/HostAprogram.sh start&quot;
}
ok: [HostB] =&gt; {
    &quot;msg&quot;: &quot;command which using host A&quot;
}
ok: [HostC] =&gt; {
    &quot;msg&quot;: &quot;/etc/init.d/HostCprogram.sh start&quot;
}
ok: [HostD] =&gt; {
    &quot;msg&quot;: &quot;command on HostD which requires HostC program to run&quot;
}
ok: [HostE] =&gt; {
    &quot;msg&quot;: &quot;command on HostE which requires HostC program to run&quot;
}