# Playbook acting on multiple hosts

**URL:** <https://forum.ansible.com/t/playbook-acting-on-multiple-hosts/13251>\
**Category:** Ansible Project\
**Created:** [August 3, 2013, 2:19pm UTC](https://forum.ansible.com/t/playbook-acting-on-multiple-hosts/13251 "2013-08-03T14:19:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sylvain\_Wallez](https://avatars.discourse-cdn.com/v4/letter/s/4da419/32.png) [@Sylvain\_Wallez](https://forum.ansible.com/u/Sylvain_Wallez)\
**Post date:** [August 3, 2013, 2:19pm UTC](https://forum.ansible.com/t/playbook-acting-on-multiple-hosts/13251/1 "2013-08-03T14:19:19Z")

</div>

Hi there,

I’ve recently fallen in love with Ansible and I’m “playbook’ing” all my install scripts. I’m currently stuck on a problem for a ninjabackup setup playbook.

Let’s consider server A that will store its backups to server B. I would like the playbook to do the following:

- set up ninjabackup on server A
- create a user “backup” on server B
- copy A’s root public ssh key to B’s backup user authorized\_keys.

I also would like B to be defined as a variable that is specific to A in order to write a generic playbook that could be used to setup cross-backup between servers A and B (and C, D…) using a host-specific variable file, e.g. “vars/A\_backup.yml”

Can a playbook have sub-playbooks allowing to specifiy a “hosts” directive containing variables defined by the parent playbook?

I am going the wrong way, having missed the obvious to achieve this?

Thanks,  
Sylvain

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [August 3, 2013, 4:07pm UTC](https://forum.ansible.com/t/playbook-acting-on-multiple-hosts/13251/2 "2013-08-03T16:07:32Z")

</div>

So if you are looking at doing one thing on one host on behalf on other hosts, you need to read up on “delegation” in the Advanced Playbook section of the manual.  
This allows you to do one thing on a server not in the host loop with a reference to the name of the other server _in_ the host loop.

You should also lookup “hostvars” which explains how to get variable information about other hosts.

---

<div class="post-metadata">

**Author:** ![Sylvain\_Wallez](https://avatars.discourse-cdn.com/v4/letter/s/4da419/32.png) [@Sylvain\_Wallez](https://forum.ansible.com/u/Sylvain_Wallez)\
**Post date:** [August 3, 2013, 6:09pm UTC](https://forum.ansible.com/t/playbook-acting-on-multiple-hosts/13251/3 "2013-08-03T18:09:10Z")

</div>

Thanks a lot Michael, “delegate\_to” is indeed what I was looking for!

When writing the task to copy the ssh pub key from server A to server B, I encountered what seems to be [https://github.com/ansible/ansible/pull/2981](https://github.com/ansible/ansible/pull/2981) (null MD5 returned on fetch used with sudo). I found this workaround using a registered variable :

- name: Backupninja | Fetch public ssh key  
command: cat /root/.ssh/id\_rsa.pub  
register: root\_pub\_key

- name: Backupninja | Add public ssh key to backup account  
delegate\_to: $backup\_host  
authorized\_key: user=$backup\_user key=“{{root\_pub\_key.stdout}}”

I also added this task to add server B’s public ssh key to server A’s known\_host file:

- name: Backupninja | Add backup host to known\_keys  
shell:  
touch ~/.ssh/known\_hosts &&  
ssh-keygen -R {{ backup\_host }} &&  
ssh-keyscan -H {{ backup\_host }} \>\> ~/.ssh/known\_hosts

Works like a charm!

Sylvain

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [August 3, 2013, 7:20pm UTC](https://forum.ansible.com/t/playbook-acting-on-multiple-hosts/13251/4 "2013-08-03T19:20:52Z")

</div>

Excellent, getting fetch to work with sudo is on our list of things to do!

–Michael

---

<div class="post-metadata">

**Author:** ![Bhotu](https://avatars.discourse-cdn.com/v4/letter/b/439d5e/32.png) [@Bhotu](https://forum.ansible.com/u/Bhotu)\
**Post date:** [November 20, 2015, 5:49pm UTC](https://forum.ansible.com/t/playbook-acting-on-multiple-hosts/13251/5 "2015-11-20T17:49:38Z")

</div>

Let us say, I have four servers. A,B,C and D. I want to deploy SSH public keys from A to C and also from C to D. How can I do with ansible playbook ?
