# How to pass 'hosts' value to an imported or included playbook?

**URL:** <https://forum.ansible.com/t/how-to-pass-hosts-value-to-an-imported-or-included-playbook/32963>\
**Category:** Ansible Project\
**Created:** [May 18, 2020, 6:44pm UTC](https://forum.ansible.com/t/how-to-pass-hosts-value-to-an-imported-or-included-playbook/32963 "2020-05-18T18:44:13Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Peter\_Bi](https://avatars.discourse-cdn.com/v4/letter/p/2bfe46/32.png) [@Peter\_Bi](https://forum.ansible.com/u/Peter_Bi)\
**Post date:** [May 18, 2020, 6:44pm UTC](https://forum.ansible.com/t/how-to-pass-hosts-value-to-an-imported-or-included-playbook/32963/1 "2020-05-18T18:44:13Z")

</div>

I’ve playbook A to include roles to conduct an operation. Now I want to create playbook B to import/include playbook A but want to pass ‘hosts’ value to A. In other words, I want to make A to work dynamically with different hosts depending on the playbooks (B/C/D/E/etc.) that include/import it.

I’ve searched around and couldn’t find a good way to do it, I’m running Ansible 2.7.10

I tried in Playbook B like [-import\_playbook playbook-A.yml my-host=‘host\_B\_name’] (in playbook A [hosts: {{‘my-host’}}]), but got error: Invalid variable name in vars specified for PlaybookInclude: ‘my-host’ is not a valid variable name

Any help or idea here?

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [May 18, 2020, 7:38pm UTC](https://forum.ansible.com/t/how-to-pass-hosts-value-to-an-imported-or-included-playbook/32963/2 "2020-05-18T19:38:19Z")

</div>

IMHO, this is easier manageable from outside of the playbook. See for example  
[https://ansible-runner.readthedocs.io/en/latest/python\_interface.html#usage-examples](https://ansible-runner.readthedocs.io/en/latest/python_interface.html#usage-examples)

It's possible to dynamically modify the inventory and change the predefined  
hosts in the Play A. For example, in Play B, use 'add\_host' to add hosts to a  
group(s) and use it in Play A  
[https://docs.ansible.com/ansible/latest/modules/add\_host\_module.html#add-host-add-a-host-and-alternatively-a-group-to-the-ansible-playbook-in-memory-inventory](https://docs.ansible.com/ansible/latest/modules/add_host_module.html#add-host-add-a-host-and-alternatively-a-group-to-the-ansible-playbook-in-memory-inventory)

&nbsp;&nbsp;- name: Play B  
&nbsp;&nbsp;&nbsp;&nbsp;hosts: hosts\_in\_play\_B  
&nbsp;&nbsp;&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- name: Add hosts to group hosts\_in\_play\_A  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;add\_host:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: '{{ item }}'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;groups: hosts\_in\_play\_A  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop: '{{ list\_of\_hosts\_in\_play\_A }}'

&nbsp;&nbsp;- name: Play A  
&nbsp;&nbsp;&nbsp;&nbsp;hosts: hosts\_in\_play\_A  
&nbsp;&nbsp;&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...

Next option, instead of 'add\_host', is to dynamically change the inventory  
and 'meta: refresh\_inventory'  
[https://docs.ansible.com/ansible/latest/modules/meta\_module.html#meta-execute-ansible-actions](https://docs.ansible.com/ansible/latest/modules/meta_module.html#meta-execute-ansible-actions)

\* At the moment, it's not possible to 'include' a playbook. There is only  
&nbsp;&nbsp;'import\_playbook'. There is no difference between importing a play and  
&nbsp;&nbsp;putting the code in-line.

\* 'import\_playbook' isn't a task.

HTH,

&nbsp;&nbsp;-vlado
