<p>Q: <em>&quot;<strong>Is it possible to use Ansible to manage the order of programs?</strong>&quot;</em></p>
<p>A: Yes. It's possible. For example, the playbook below does the job</p>
<pre class="lang-yaml prettyprint-override"><code>- 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)
</code></pre>
<hr>
<p>Example of the roles for the purpose of testing</p>
<pre class="lang-yaml prettyprint-override"><code>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
</code></pre>
<p>Example of the output</p>
<pre class="lang-yaml prettyprint-override"><code>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;
}
</code></pre>

CC BY-SA 4.0
2020-02-02T23:08:00.167Z
2021-03-26T08:48:24.410Z
2021-03-26T08:48:24.410Z
Vladimir Botka
<p>Is it possible to use ansible to manage order of programs?
Im trying to find an automated solution which will do the following steps:</p>

<ol>
<li>run a program on HostA</li>
<li>run a command on HostB which requires HostA program to run</li>
<li>run a program on HostC if 2. step succeed</li>
<li>run a command on HostD which requires HostC program to run</li>
<li>run a command on HostE which requires HostC program to run</li>
</ol>

<p>For example:</p>

<pre><code>---
  - hosts: neededhosts
    roles:
      - startprogram
      - runcommand
      - startsecprgoram
    .
    .
    .

</code></pre>

<pre><code>startprogram:
- name: start hostA program
  tasks:
    - name: start program
      hosts: HostA
      shell: /etc/init.d/HostAprogram.sh start
</code></pre>

<pre><code>runcommand:
- name: run command on HostB
  tasks:
    - name: run command
      hosts: HostB
      shell: command which using host A
</code></pre>

<ansible>
Ansible execute task on different hosts in order
Richie G. Edward
# Ansible execute task on different hosts in order

(Asked by: username_0 on 2020-02-02)

Is it possible to use ansible to manage order of programs?
Im trying to find an automated solution which will do the following steps:

run a program on HostA
run a command on HostB which requires HostA program to run
run a program on HostC if 2. step succeed
run a command on HostD which requires HostC program to run
run a command on HostE which requires HostC program to run

For example:
---
  - hosts: neededhosts
    roles:
      - startprogram
      - runcommand
      - startsecprgoram
    .
    .
    .


startprogram:
- name: start hostA program
  tasks:
    - name: start program
      hosts: HostA
      shell: /etc/init.d/HostAprogram.sh start

runcommand:
- name: run command on HostB
  tasks:
    - name: run command
      hosts: HostB
      shell: command which using host A

**Tags:** ansible


---

## 1 answers

### Answer by username_1 on 2020-02-02. Score: 8 | Accepted answer

Q: "Is it possible to use Ansible to manage the order of programs?"
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> cat roles/startprogram/tasks/main.yml
- debug:
    msg: /etc/init.d/HostAprogram.sh start
- set_fact:
    HostAprogram_started: true

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

shell> 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] => {
    "msg": "/etc/init.d/HostAprogram.sh start"
}
ok: [HostB] => {
    "msg": "command which using host A"
}
ok: [HostC] => {
    "msg": "/etc/init.d/HostCprogram.sh start"
}
ok: [HostD] => {
    "msg": "command on HostD which requires HostC program to run"
}
ok: [HostE] => {
    "msg": "command on HostE which requires HostC program to run"
}


---

