<p>If I've understood your problem correctly:</p>

<ul>
<li>the <code>ansible</code> shell command in the middle could be converted to a simple playbook using the <code>command</code> task - let's call this <code>middle.yml</code></li>
<li>your existing playbook could then directly import this <code>middle.yml</code> play (or even a more complex playbook).  In Ansible 2.4+, you would use <a href="http://docs.ansible.com/ansible/latest/playbooks_reuse_includes.html#importing-playbooks" rel="noreferrer">import_playbook</a>, specifically <code>import_playbook: middle.yml</code>, or in earlier versions, just <code>include: middle.yml</code>.</li>
</ul>

<p>This processes the whole <code>middle.yml</code> playbook as if it was part of the main playbook - you have all the features of Ansible plays (<code>hosts</code> etc) and the output appears as part of main playbook.</p>

<p>This answer may need updating depending on your response to my comment above.</p>

CC BY-SA 3.0
2017-11-24T14:12:45.793Z
Looking into some of the docs, I discovered something I'd tried before, but forgot about - putting multiple plays inside the same playbook (should have figured that was obvious since it's a playbook!)  but I think that'll accomplish what I want perfectly.  My only problem is that the device I'm running ansible on can't directly reach the devices I want to mess with, but that's not a show stopper.
Peter Turner
CC BY-SA 3.0
2017-11-22T21:32:37.920Z
2017-11-22T21:32:37.920Z
RichVel
<p>Take a look at <a href="http://docs.ansible.com/ansible/latest/playbooks_delegation.html#delegation" rel="nofollow noreferrer">Delegation</a> - you can delegate the execution of tasks to a different host from within a playbook:</p>

<pre><code>  - name: enable the server in haproxy
    haproxy: 'state=enabled backend=myapplb host={{ inventory_hostname }} socket=/var/lib/haproxy/stats'
    delegate_to: "{{ item }}"
    with_items: groups.lbservers
</code></pre>

CC BY-SA 3.0
2018-03-09T20:18:44.130Z
2018-03-09T20:18:44.130Z
niglesias
<p>I have playbook that I've been hacking on for a year and it works OK, but I know it's not great, part of it runs ansible on the target machine with a "command" task.  Because of that, I don't see the output of this task in my ansible playbook.  </p>

<p>I need to run this play in the middle of the playbook because it is dependent on a prior task and a subsequent task depends on it (otherwise I'd break it up differently) </p>

<p>I'd like to be able to see the output of all my plays, is there a better way to branch off plays in the middle of a play.  </p>

<p>For instance: given a playbook running on HOST_A and HOST_B at TASK 3, suspend running plays, run next plays on HOST_C and HOST_D then HOST_E and HOST_F? </p>

<pre><code>- name: update other machines
  command: "/usr/bin/ansible-playbook /etc/ansible/playbooks/other-machines.yml -e 'ansible_user={{ item.ansible_user }}' -l {{ item.ansible_hostname }}"
  with_items: "{{ other_machine_connections }}"
</code></pre>

CC BY-SA 3.0
2017-11-22T21:28:15.723Z
Does the task that you run with `ansible` include `-m command -s 'some commands'`, i.e. you aren't actually running a playbook?  In your last example, is it one play for HOST_A and HOST_B, then run `ansible` command on HOST_C and HOST_D, then final play on HOST_E and HOST_F?  Examples of the plays and commands would help a lot, even if you include dummy code, to understand structure.
RichVel
CC BY-SA 3.0
2017-11-23T06:04:55.963Z
A piece of code would greatly enrich your question...
Konstantin Suvorov
CC BY-SA 3.0
2017-11-24T14:00:09.290Z
@RichVel It's pretty simple and as you describe, except I'm using `ansible-playbook` instead of just `ansible`
Peter Turner
<ansible>
What is the best strategy for running an ansible playbook within an ansible playbook?
Peter Turner
# What is the best strategy for running an ansible playbook within an ansible playbook?

(Asked by: username_0 on 2017-11-22)

I have playbook that I've been hacking on for a year and it works OK, but I know it's not great, part of it runs ansible on the target machine with a "command" task.  Because of that, I don't see the output of this task in my ansible playbook.  
I need to run this play in the middle of the playbook because it is dependent on a prior task and a subsequent task depends on it (otherwise I'd break it up differently) 
I'd like to be able to see the output of all my plays, is there a better way to branch off plays in the middle of a play.  
For instance: given a playbook running on HOST_A and HOST_B at TASK 3, suspend running plays, run next plays on HOST_C and HOST_D then HOST_E and HOST_F? 
- name: update other machines
  command: "/usr/bin/ansible-playbook /etc/ansible/playbooks/other-machines.yml -e 'ansible_user={{ item.ansible_user }}' -l {{ item.ansible_hostname }}"
  with_items: "{{ other_machine_connections }}"

**Tags:** ansible

> **Comments:**
> **username_1**: Does the task that you run with `ansible` include `-m command -s 'some commands'`, i.e. you aren't actually running a playbook?  In your last example, is it one play for HOST_A and HOST_B, then run `ansible` command on HOST_C and HOST_D, then final play on HOST_E and HOST_F?  Examples of the plays and commands would help a lot, even if you include dummy code, to understand structure.  
> **username_2**: A piece of code would greatly enrich your question...  
> **username_0**: @username_1 It's pretty simple and as you describe, except I'm using `ansible-playbook` instead of just `ansible`  

---

## 2 answers

### Answer by username_1 on 2017-11-22. Score: 5

If I've understood your problem correctly:

* the `ansible` shell command in the middle could be converted to a simple playbook using the `command` task - let's call this `middle.yml`
* your existing playbook could then directly import this `middle.yml` play (or even a more complex playbook). In Ansible 2.4+, you would use [import\_playbook](http://docs.ansible.com/ansible/latest/playbooks_reuse_includes.html#importing-playbooks), specifically `import_playbook: middle.yml`, or in earlier versions, just `include: middle.yml`.

This processes the whole `middle.yml` playbook as if it was part of the main playbook - you have all the features of Ansible plays (`hosts` etc) and the output appears as part of main playbook.

This answer may need updating depending on your response to my comment above.

> **Comments:**
> **username_0**: Looking into some of the docs, I discovered something I'd tried before, but forgot about - putting multiple plays inside the same playbook (should have figured that was obvious since it's a playbook!)  but I think that'll accomplish what I want perfectly.  My only problem is that the device I'm running ansible on can't directly reach the devices I want to mess with, but that's not a show stopper.  

---

### Answer by username_3 on 2018-03-09. Score: 3

Take a look at Delegation - you can delegate the execution of tasks to a different host from within a playbook:
  - name: enable the server in haproxy
    haproxy: 'state=enabled backend=myapplb host={{ inventory_hostname }} socket=/var/lib/haproxy/stats'
    delegate_to: "{{ item }}"
    with_items: groups.lbservers


---

