<p>It depends mostly on <a href="https://docs.ansible.com/ansible/latest/inventory_guide/intro_inventory.html" rel="nofollow noreferrer">How one build his inventory</a> and has <a href="https://docs.ansible.com/ansible/latest/inventory_guide/intro_inventory.html#organizing-host-and-group-variables" rel="nofollow noreferrer">organized hosts and group variables</a>. As well what kind of services and configuration needs to be addressed and if there are dependencies between them.</p>
<p>One of many possibilities is for an inventory file <strong><code>example.com</code></strong></p>
<pre class="lang-ini prettyprint-override"><code>[tenants:children]
A
B

[A]
a.example.com
b.example.com
c.example.com

[B]
a.example.com
d.example.com
</code></pre>
<p>a minimal example playbook</p>
<pre class="lang-yaml prettyprint-override"><code>---
- hosts: localhost
  become: false
  gather_facts: false

  pre_tasks:

  - name: Create configurations, store it in a Git repo and push it into Bitbucket
    debug:
      msg: &quot;1&quot;

  - name: Setup a Git repo with basic configs and push it into Bitbucket
    debug:
      msg: &quot;2&quot;

  - name: Setup some other configs and post it with HTTP to a service
    debug:
      msg: &quot;3&quot;

- hosts: A
  become: false
  gather_facts: false

  tasks:

  - name: Create a basic filesystem (directory and files)
    debug:
      msg: &quot;4.1&quot;

  - name: Restart a daemon
    debug:
      msg: &quot;5.1&quot;

- hosts: B
  become: false
  gather_facts: false

  tasks:

  - name: Create a basic filesystem (directory and files)
    debug:
      msg: &quot;4.2&quot;

  - name: Restart a daemon
    debug:
      msg: &quot;5.2&quot;
</code></pre>
<p>Called via</p>
<pre class="lang-sh prettyprint-override"><code>ansible-playbook --inventory example.com main.yml
</code></pre>
<p>it will result into an output of</p>
<pre class="lang-yaml prettyprint-override"><code>PLAY [localhost] ************************************************************************************************************************************************

TASK [Create configurations, store it in a Git repo and push it into Bitbucket] *********************************************************************************
ok: [localhost] =&gt;
  msg: '1'

TASK [Setup a Git repo with basic configs and push it into Bitbucket] *******************************************************************************************
ok: [localhost] =&gt;
  msg: '2'

TASK [Setup some other configs and post it with HTTP to a service] **********************************************************************************************
ok: [localhost] =&gt;
  msg: '3'

PLAY [A] ********************************************************************************************************************************************************

TASK [Create a basic filesystem (directory and files)] **********************************************************************************************************
ok: [a.example.com] =&gt;
  msg: '4.1'
ok: [b.example.com] =&gt;
  msg: '4.1'
ok: [c.example.com] =&gt;
  msg: '4.1'

TASK [Restart a daemon] *****************************************************************************************************************************************
ok: [a.example.com] =&gt;
  msg: '5.1'
ok: [b.example.com] =&gt;
  msg: '5.1'
ok: [c.example.com] =&gt;
  msg: '5.1'

PLAY [B] ********************************************************************************************************************************************************

TASK [Create a basic filesystem (directory and files)] **********************************************************************************************************
ok: [a.example.com] =&gt;
  msg: '4.2'
ok: [d.example.com] =&gt;
  msg: '4.2'

TASK [Restart a daemon] *****************************************************************************************************************************************
ok: [a.example.com] =&gt;
  msg: '5.2'
ok: [d.example.com] =&gt;
  msg: '5.2'
</code></pre>

CC BY-SA 4.0
2024-02-27T09:02:50.007Z
It might be worth to mention why did you used `pre_tasks` in the first play or replace it with `tasks`. Since there is no `roles`, I guess it might be misinterpreted by novices.
Alexander Pletnev
CC BY-SA 4.0
2024-02-27T09:35:48.957Z
@AlexanderPletnev, right, a hint regarding [`pre_tasks` should be linked](https://www.redhat.com/sysadmin/ansible-pretasks-posttasks) here as it will help one if going to implement one of the many other possibilities with [`Roles`](https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_reuse_roles.html).
U880D
CC BY-SA 4.0
2024-02-26T15:17:17.853Z
2024-02-26T15:17:17.853Z
U880D
<p>I am new to ansible and I am wondering how I should design my workflow with ansible in an ansible kind of way.</p>
<p><strong>The Task:</strong>  I have a workflow/process (not ansible yet) for setting an application up. This workflow has to do serveral tasks on different hosts and to store some configuration in git repositories. Depending on the tenend (e.g. A and B) of my application, it has to setup different stuff on different hosts. My workflow from top to down:</p>
<div class="s-table-container"><table class="s-table">
<thead>
<tr>
<th>#</th>
<th>host</th>
<th>tenand</th>
<th>task description</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>localhost</td>
<td>A &amp; B</td>
<td>Create configurations and store it in an git repo and push to bitbucket</td>
</tr>
<tr>
<td>2</td>
<td>localhost</td>
<td>A &amp; B</td>
<td>Setup an git repo with basic configs and push to bitbucket</td>
</tr>
<tr>
<td>3</td>
<td>localhost</td>
<td>A &amp; B</td>
<td>Setup some other configs and post it with HTTP to a service</td>
</tr>
<tr>
<td>4.1</td>
<td>host a,b,c</td>
<td>A</td>
<td>Create a basic filesystem (directory and files)</td>
</tr>
<tr>
<td>4.2</td>
<td>host a,d</td>
<td>B</td>
<td>Create a basic filesystem (directory and files)</td>
</tr>
<tr>
<td>5.1</td>
<td>host a,b,c</td>
<td>A</td>
<td>Restart a deamon</td>
</tr>
<tr>
<td>5.2</td>
<td>host a,d</td>
<td>B</td>
<td>Restart a deamon</td>
</tr>
</tbody>
</table></div>
<p><strong>Thoughts</strong>:
I did create a playbook in exactly this manner and tried to differ between the hosts depending on the tenand an in its own configuration file. However thats exactly the way I am used to by an workflow orchestraton tool. Is it the ansible way too?<br />
Do I have to change my perspective to the hosts and should create a playbook for each host and decide during the runtime if there are tasks for this host?</p>

<ansible>
How to design an ansible workflow over multiple hosts?
Mike
# How to design an ansible workflow over multiple hosts?

(Asked by: username_0 on 2024-02-16)

I am new to ansible and I am wondering how I should design my workflow with ansible in an ansible kind of way.

**The Task:** I have a workflow/process (not ansible yet) for setting an application up. This workflow has to do serveral tasks on different hosts and to store some configuration in git repositories. Depending on the tenend (e.g. A and B) of my application, it has to setup different stuff on different hosts. My workflow from top to down:

| # | host | tenand | task description |
| --- | --- | --- | --- |
| 1 | localhost | A & B | Create configurations and store it in an git repo and push to bitbucket |
| 2 | localhost | A & B | Setup an git repo with basic configs and push to bitbucket |
| 3 | localhost | A & B | Setup some other configs and post it with HTTP to a service |
| 4.1 | host a,b,c | A | Create a basic filesystem (directory and files) |
| 4.2 | host a,d | B | Create a basic filesystem (directory and files) |
| 5.1 | host a,b,c | A | Restart a deamon |
| 5.2 | host a,d | B | Restart a deamon |

**Thoughts**:
I did create a playbook in exactly this manner and tried to differ between the hosts depending on the tenand an in its own configuration file. However thats exactly the way I am used to by an workflow orchestraton tool. Is it the ansible way too?  
Do I have to change my perspective to the hosts and should create a playbook for each host and decide during the runtime if there are tasks for this host?

**Tags:** ansible


---

## 1 answers

### Answer by username_1 on 2024-02-26. Score: 0

It depends mostly on How one build his inventory and has organized hosts and group variables. As well what kind of services and configuration needs to be addressed and if there are dependencies between them.
One of many possibilities is for an inventory file example.com
[tenants:children]
A
B

[A]
a.example.com
b.example.com
c.example.com

[B]
a.example.com
d.example.com

a minimal example playbook
---
- hosts: localhost
  become: false
  gather_facts: false

  pre_tasks:

  - name: Create configurations, store it in a Git repo and push it into Bitbucket
    debug:
      msg: "1"

  - name: Setup a Git repo with basic configs and push it into Bitbucket
    debug:
      msg: "2"

  - name: Setup some other configs and post it with HTTP to a service
    debug:
      msg: "3"

- hosts: A
  become: false
  gather_facts: false

  tasks:

  - name: Create a basic filesystem (directory and files)
    debug:
      msg: "4.1"

  - name: Restart a daemon
    debug:
      msg: "5.1"

- hosts: B
  become: false
  gather_facts: false

  tasks:

  - name: Create a basic filesystem (directory and files)
    debug:
      msg: "4.2"

  - name: Restart a daemon
    debug:
      msg: "5.2"

Called via
ansible-playbook --inventory example.com main.yml

it will result into an output of
PLAY [localhost] ************************************************************************************************************************************************

TASK [Create configurations, store it in a Git repo and push it into Bitbucket] *********************************************************************************
ok: [localhost] =>
  msg: '1'

TASK [Setup a Git repo with basic configs and push it into Bitbucket] *******************************************************************************************
ok: [localhost] =>
  msg: '2'

TASK [Setup some other configs and post it with HTTP to a service] **********************************************************************************************
ok: [localhost] =>
  msg: '3'

PLAY [A] ********************************************************************************************************************************************************

TASK [Create a basic filesystem (directory and files)] **********************************************************************************************************
ok: [a.example.com] =>
  msg: '4.1'
ok: [b.example.com] =>
  msg: '4.1'
ok: [c.example.com] =>
  msg: '4.1'

TASK [Restart a daemon] *****************************************************************************************************************************************
ok: [a.example.com] =>
  msg: '5.1'
ok: [b.example.com] =>
  msg: '5.1'
ok: [c.example.com] =>
  msg: '5.1'

PLAY [B] ********************************************************************************************************************************************************

TASK [Create a basic filesystem (directory and files)] **********************************************************************************************************
ok: [a.example.com] =>
  msg: '4.2'
ok: [d.example.com] =>
  msg: '4.2'

TASK [Restart a daemon] *****************************************************************************************************************************************
ok: [a.example.com] =>
  msg: '5.2'
ok: [d.example.com] =>
  msg: '5.2'

> **Comments:**
> **username_2**: It might be worth to mention why did you used `pre_tasks` in the first play or replace it with `tasks`. Since there is no `roles`, I guess it might be misinterpreted by novices.  
> **username_1**: @AlexanderPletnev, right, a hint regarding [`pre_tasks` should be linked](https://www.redhat.com/sysadmin/ansible-pretasks-posttasks) here as it will help one if going to implement one of the many other possibilities with [`Roles`](https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_reuse_roles.html).  

---

