# Delegating a playbook to run across multiple hosts

**URL:** <https://forum.ansible.com/t/delegating-a-playbook-to-run-across-multiple-hosts/23789>\
**Category:** Ansible Project\
**Created:** [February 21, 2017, 4:13pm UTC](https://forum.ansible.com/t/delegating-a-playbook-to-run-across-multiple-hosts/23789 "2017-02-21T16:13:41Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![barnaclefluff](https://avatars.discourse-cdn.com/v4/letter/b/8edcca/32.png) [@barnaclefluff](https://forum.ansible.com/u/barnaclefluff)\
**Post date:** [February 21, 2017, 4:13pm UTC](https://forum.ansible.com/t/delegating-a-playbook-to-run-across-multiple-hosts/23789/1 "2017-02-21T16:13:41Z")

</div>

I’m having a newbie issue related to orchestrating actions across hosts. I’m probably doing this completely the wrong way.

Essentially what I want to do is call a playbook from within a playbook, targeting specific hosts.

An example use case is I add an exported dir on an NFS server, then I want to log into the clients I’ve exported it to and mount the new share.  
Or, I make some DB change and now need to connect to all the impacted application servers to tell them to reconnect to the DB.  
Or, I’m patching servers in a load balanced server farm, so I want to first find the nodes and then iterate a patch/reboot cycle on each sequentially to avoid service downtime.

Basically, within my playbook I’m discovering the list of target hosts, and I figured I could just pass that list to an included playbook like so:

- name: Call playbook to do something on a list of hosts in sequence  
include: something.yml  
delegate\_to: “{{ item }}”  
with\_items: “{{ my\_target\_hosts }}”

That seems to work, but something.yml will inherit all facts. For example “ansible\_hostname” from within something.yml will reflect the original host, not the one where it’s running.  
Then I should call re-run setup, I suppose? But if I do that, it will, for reasons I don’t understand, skip all tasks after setup re-ran.

Example something.yml when my\_target\_hosts are host1 and host2 - notice the last two tasks are skipped:

- debug:  
msg: “i am here”

- debug:  
msg: “my ansible\_hostname is now {{ ansible\_hostname }}”

- name: Call setup again  
setup:

- debug:  
msg: “ive run setup again and now my ansible\_hostname is {{ ansible\_hostname }}”

- debug:  
msg: “i am done”

Results in:

TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [host1 → host1] =\> {  
“msg”: “i am here”  
}

TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [host1 → host1] =\> {  
“msg”: “my ansible\_hostname is now host1”  
}

TASK [Call setup again] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [host1 → host1]

TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [host1 → host1] =\> {  
“msg”: “ive run setup again and now my ansible\_hostname is host1”  
}

TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [host1 → host1] =\> {  
“msg”: “i am done”  
}

TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [host1 → host2] =\> {  
“msg”: “i am here”  
}

TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [host1 → host2] =\> {  
“msg”: “my ansible\_hostname is now host1”  
}

TASK [Call setup again] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [host1 → host2]

TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
skipping: [host1] =\> {“changed”: false, “skip\_reason”: “Conditional check failed”, “skipped”: true}

TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
skipping: [host1] =\> {“changed”: false, “skip\_reason”: “Conditional check failed”, “skipped”: true}

Where’s my brainfart?
