# Change hosts variable when including another playbook

**URL:** <https://forum.ansible.com/t/change-hosts-variable-when-including-another-playbook/22634>\
**Category:** Ansible Project\
**Created:** [September 23, 2016, 8:44pm UTC](https://forum.ansible.com/t/change-hosts-variable-when-including-another-playbook/22634 "2016-09-23T20:44:17Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tristan\_Muntsinger](https://avatars.discourse-cdn.com/v4/letter/t/2acd7d/32.png) [@Tristan\_Muntsinger](https://forum.ansible.com/u/Tristan_Muntsinger)\
**Post date:** [September 23, 2016, 8:44pm UTC](https://forum.ansible.com/t/change-hosts-variable-when-including-another-playbook/22634/1 "2016-09-23T20:44:17Z")

</div>

How do I change hosts when including tasks from another playbook? I’ve tried both methods I could find below, but neither seems to work:

`

- name: example  
hosts: localhost  
tasks:
- name: execute this task  
include: tasks/task1.yml  
hosts: “{{ name }}.example.com”  
`

`

- name: example  
hosts: localhost  
tasks:
- name: execute this task  
include: tasks/task1.yml hosts={{ name }}.example.com  
`

-Tristan

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [September 24, 2016, 12:48am UTC](https://forum.ansible.com/t/change-hosts-variable-when-including-another-playbook/22634/2 "2016-09-24T00:48:50Z")

</div>

you don’t, you create a new play that targets those hosts:

---

<div class="post-metadata">

**Author:** ![Tristan\_Muntsinger](https://avatars.discourse-cdn.com/v4/letter/t/2acd7d/32.png) [@Tristan\_Muntsinger](https://forum.ansible.com/u/Tristan_Muntsinger)\
**Post date:** [September 24, 2016, 12:58am UTC](https://forum.ansible.com/t/change-hosts-variable-when-including-another-playbook/22634/3 "2016-09-24T00:58:23Z")

</div>

I need to be able to do this from a single playbook. What if I create a task that is a shell command that calls into ansible-playbook on a different playbook?

-Tristan

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [September 24, 2016, 3:28am UTC](https://forum.ansible.com/t/change-hosts-variable-when-including-another-playbook/22634/4 "2016-09-24T03:28:25Z")

</div>

A playbook can have multiple plays:

- hosts: host\_for\_play\_1  
tasks:
- debug: msg=‘play 1 task’  
​​ …

​

​- hosts: host\_for\_play\_2  
tasks:

- debug: msg='play 2 task’​  
…

---

<div class="post-metadata">

**Author:** ![Tristan\_Muntsinger](https://avatars.discourse-cdn.com/v4/letter/t/2acd7d/32.png) [@Tristan\_Muntsinger](https://forum.ansible.com/u/Tristan_Muntsinger)\
**Post date:** [September 26, 2016, 5:53pm UTC](https://forum.ansible.com/t/change-hosts-variable-when-including-another-playbook/22634/5 "2016-09-26T17:53:32Z")

</div>

I understand that, but it seems that each play can’t have a different hosts set. That is, I would like the first play to have “localhost” as the hosts and the second play to have hosts dynamically set to whatever the first play says it is.

-Tristan

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [September 26, 2016, 6:50pm UTC](https://forum.ansible.com/t/change-hosts-variable-when-including-another-playbook/22634/6 "2016-09-26T18:50:52Z")

</div>

This it possible to do, see example below.

---

<div class="post-metadata">

**Author:** ![Tristan\_Muntsinger](https://avatars.discourse-cdn.com/v4/letter/t/2acd7d/32.png) [@Tristan\_Muntsinger](https://forum.ansible.com/u/Tristan_Muntsinger)\
**Post date:** [September 30, 2016, 12:30am UTC](https://forum.ansible.com/t/change-hosts-variable-when-including-another-playbook/22634/7 "2016-09-30T00:30:44Z")

</div>

Using your example (and replacing ‘a1’ with the host I care about) doesn’t appear to work. This is what I see:

PLAY [test] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
skipping: no hosts matched

When I directly copy/paste the hostname into hosts in the second play, it works as expected.

-Tristan

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [September 30, 2016, 5:07pm UTC](https://forum.ansible.com/t/change-hosts-variable-when-including-another-playbook/22634/8 "2016-09-30T17:07:10Z")

</div>

It works for me with Ansible 2.1.1.

---

<div class="post-metadata">

**Author:** ![Tristan\_Muntsinger](https://avatars.discourse-cdn.com/v4/letter/t/2acd7d/32.png) [@Tristan\_Muntsinger](https://forum.ansible.com/u/Tristan_Muntsinger)\
**Post date:** [October 1, 2016, 12:38am UTC](https://forum.ansible.com/t/change-hosts-variable-when-including-another-playbook/22634/9 "2016-10-01T00:38:23Z")

</div>

Maybe the issue is that I’m using 1.9.1-1

I was able to finally resolve my issue by doing the below:

---

<div class="post-metadata">

**Author:** ![Mike\_Schlottman](https://avatars.discourse-cdn.com/v4/letter/m/97f17d/32.png) [@Mike\_Schlottman](https://forum.ansible.com/u/Mike_Schlottman)\
**Post date:** [October 3, 2016, 11:44pm UTC](https://forum.ansible.com/t/change-hosts-variable-when-including-another-playbook/22634/10 "2016-10-03T23:44:35Z")

</div>

I do this by setting the hosts line in a playbook to a variable. I’ve been working with virtual machines a lot lately, so the variable I use is vms.

---

<div class="post-metadata">

**Author:** ![Aditya\_Garg](https://avatars.discourse-cdn.com/v4/letter/a/13edae/32.png) [@Aditya\_Garg](https://forum.ansible.com/u/Aditya_Garg)\
**Post date:** [April 13, 2020, 7:10am UTC](https://forum.ansible.com/t/change-hosts-variable-when-including-another-playbook/22634/11 "2020-04-13T07:10:50Z")

</div>

What will be the command to pass username and password for both the host considering none of the host is localhost.
