# Run playbook against multiple hosts

**URL:** <https://forum.ansible.com/t/run-playbook-against-multiple-hosts/36389>\
**Category:** Ansible Project\
**Tags:** windows\
**Created:** [March 1, 2022, 3:51pm UTC](https://forum.ansible.com/t/run-playbook-against-multiple-hosts/36389 "2022-03-01T15:51:15Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nitrous](https://avatars.discourse-cdn.com/v4/letter/n/dfb087/32.png) [@Nitrous](https://forum.ansible.com/u/Nitrous)\
**Post date:** [March 1, 2022, 3:51pm UTC](https://forum.ansible.com/t/run-playbook-against-multiple-hosts/36389/1 "2022-03-01T15:51:15Z")

</div>

I have defined my hosts in the variable file:

[servers]

server1 ansible\_host=ip address of server 1

server2 ansible\_host=ip address of server 2

etc

I have a playbook that looks like this:

- hosts: ‘{{ hostname }}’  
gather\_facts: no  
tasks:

- name: Include vars for vcenter  
include\_vars:  
file: /etc/ansible/roles/createvm/vars/main.yml  
name: vcenter  
vars:  
ansible\_become\_password: “{{ domain\_password }}”

- name: Include vars of server.yaml file  
include\_vars:  
file: /etc/ansible/servers/{{ hostname }}.yaml  
name: server

- name: Wait For Connection to Continue  
wait\_for\_connection:  
connect\_timeout: 30

- name: Reboot to check for second round of updates  
ansible.windows.win\_reboot:  
msg: “Reboot to check for second round of updates”

- import\_role:  
name: windowsupdates

This playbook works fine if I run the command to specify 1 hostname:

ansible-playbook playbooks/file.yml -e “hostname=server1” -vvvv.

How can I run my file.yml against server1,server2 etc at the same time?

Thanks

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [March 1, 2022, 4:14pm UTC](https://forum.ansible.com/t/run-playbook-against-multiple-hosts/36389/2 "2022-03-01T16:14:06Z")

</div>

> I have defined my hosts in the variable file:
> 
> [servers]
> 
> server1 ansible\_host=ip address of server 1
> 
> server2 ansible\_host=ip address of server 2
> 
> etc
> 
> I have a playbook that looks like this:
> 
> - hosts: '{{ hostname }}'  
> gather\_facts: no  
> tasks:  
> - name: Include vars for vcenter  
> include\_vars:  
> file: /etc/ansible/roles/createvm/vars/main.yml  
> name: vcenter  
> vars:  
> ansible\_become\_password: "{{ domain\_password }}"
> 
> - name: Include vars of server.yaml file  
> include\_vars:  
> file: /etc/ansible/servers/{{ hostname }}.yaml  
> name: server
> 
> - name: Wait For Connection to Continue  
> wait\_for\_connection:  
> connect\_timeout: 30
> 
> - name: Reboot to check for second round of updates  
> ansible.windows.win\_reboot:  
> msg: "Reboot to check for second round of updates"
> 
> - import\_role:  
> name: windowsupdates
> 
> This playbook works fine if I run the command to specify 1 hostname:
> 
> ansible-playbook playbooks/file.yml -e "hostname=server1" -vvvv.
> 
> How can I run my file.yml against server1,server2 etc at the same time?

I suppose that -e "hostname=servers" should do the trick.

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Mike\_Eggleston](https://avatars.discourse-cdn.com/v4/letter/m/58956e/32.png) [@Mike\_Eggleston](https://forum.ansible.com/u/Mike_Eggleston)\
**Post date:** [March 1, 2022, 4:21pm UTC](https://forum.ansible.com/t/run-playbook-against-multiple-hosts/36389/3 "2022-03-01T16:21:55Z")

</div>

I must have missed something… why wouldn’t -i $FILE not work?

---

<div class="post-metadata">

**Author:** ![Nitrous](https://avatars.discourse-cdn.com/v4/letter/n/dfb087/32.png) [@Nitrous](https://forum.ansible.com/u/Nitrous)\
**Post date:** [March 1, 2022, 6:14pm UTC](https://forum.ansible.com/t/run-playbook-against-multiple-hosts/36389/4 "2022-03-01T18:14:26Z")

</div>

This is what I got when I ran:

TASK [Include vars of server.yaml file] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
task path: /etc/ansible/roles/secwinupdates/tasks/main.yml:12  
fatal: [server1]: FAILED! =\> {  
“ansible\_facts”: {  
“server”: {}  
},  
“ansible\_included\_var\_files”: ,  
“changed”: false,  
“message”: “Could not find or access ‘/etc/ansible/servers/win\_us\_bgrsuistg.yaml’ on the Ansible Controller.\nIf you are using a module and expect the file to exist on the remote, see the remote\_src option”  
}  
fatal: [server2]: FAILED! =\> {  
“ansible\_facts”: {  
“server”: {}  
},  
“ansible\_included\_var\_files”: ,  
“changed”: false,  
“message”: “Could not find or access ‘/etc/ansible/servers/win\_us\_bgrsuistg.yaml’ on the Ansible Controller.\nIf you are using a module and expect the file to exist on the remote, see the remote\_src option”  
}  
fatal: [server3]: FAILED! =\> {  
“ansible\_facts”: {  
“server”: {}  
},  
“ansible\_included\_var\_files”: ,  
“changed”: false,  
“message”: “Could not find or access ‘/etc/ansible/servers/win\_us\_bgrsuistg.yaml’ on the Ansible Controller.\nIf you are using a module and expect the file to exist on the remote, see the remote\_src option”  
}

This is what my host file looks like:

[win\_us\_bgrsuistg]  
server1 ansible\_host=x.x.x.x

server2 ansible\_host=x.x.x.x  
server3 ansible\_host=x.x.x.x

I tried to create a seperate yaml file for the servers, called win\_us\_bgrsuistg , which includes the server1, server2, server 3 names in it, I then got the below error:

fatal: [server1]: FAILED! =\> {  
“ansible\_facts”: {  
“server”: {}  
},  
“ansible\_included\_var\_files”: ,  
“changed”: false,  
“message”: “/etc/ansible/servers/win\_us\_bgrsuistg.yaml must be stored as a dictionary/hash”

---

<div class="post-metadata">

**Author:** ![Nitrous](https://avatars.discourse-cdn.com/v4/letter/n/dfb087/32.png) [@Nitrous](https://forum.ansible.com/u/Nitrous)\
**Post date:** [March 1, 2022, 6:26pm UTC](https://forum.ansible.com/t/run-playbook-against-multiple-hosts/36389/5 "2022-03-01T18:26:28Z")

</div>

Does my win\_us\_bgrsuistg.yaml file have to be in a specific format, maybe its missing some information, that I am overlooking.

---

<div class="post-metadata">

**Author:** ![Nitrous](https://avatars.discourse-cdn.com/v4/letter/n/dfb087/32.png) [@Nitrous](https://forum.ansible.com/u/Nitrous)\
**Post date:** [March 2, 2022, 3:28pm UTC](https://forum.ansible.com/t/run-playbook-against-multiple-hosts/36389/6 "2022-03-02T15:28:55Z")

</div>

I got it work, by following this simple example on git hub:

[https://github.com/ryanschulze/ansible\_examples/tree/master/simple](https://github.com/ryanschulze/ansible_examples/tree/master/simple)

Thanks everyone.
