# Geting vars from a different playbook to use as the "hosts" value

**URL:** <https://forum.ansible.com/t/geting-vars-from-a-different-playbook-to-use-as-the-hosts-value/15938>\
**Category:** Ansible Project\
**Tags:** ubuntu, aws\
**Created:** [August 13, 2014, 11:48pm UTC](https://forum.ansible.com/t/geting-vars-from-a-different-playbook-to-use-as-the-hosts-value/15938 "2014-08-13T23:48:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shih\_Oon\_Liong2](https://avatars.discourse-cdn.com/v4/letter/s/90ced4/32.png) [@Shih\_Oon\_Liong2](https://forum.ansible.com/u/Shih_Oon_Liong2)\
**Post date:** [August 13, 2014, 11:48pm UTC](https://forum.ansible.com/t/geting-vars-from-a-different-playbook-to-use-as-the-hosts-value/15938/1 "2014-08-13T23:48:52Z")

</div>

I am trying to run two playbooks at the same time, with one taking a var from the previous playbook.

My code is as follows

> - name: Provision AWS  
> hosts: localhost  
> gather\_facts: no  
> vars\_files:
> - configs/aws.yml  
> tasks:
> - debug: msg=‘tag\_Name\_{{ aws.ec2.Name }}’  
> register: aws\_host\_target
> - hosts: {{ hostvars.localhost.aws\_host\_target.msg  
> remote\_user: ubuntu  
> sudo: Trues  
> tasks:
> - debug: var=hostvars.localhost.aws\_host\_target.msg

Yet everytime it hits the second playbook, the variable aws\_host\_target.msg is not readable specifically for the “hosts”

Any help would be great!

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [August 14, 2014, 11:52am UTC](https://forum.ansible.com/t/geting-vars-from-a-different-playbook-to-use-as-the-hosts-value/15938/2 "2014-08-14T11:52:36Z")

</div>

It’s not possible to use inventory variables in the “host” spec (“- hosts:”).

Also, don’t use debug with register, instead, if you need to set a variable:

- set\_fact:  
foo: “tag\_Name\_{{ [aws.ec2.Name](http://aws.ec2.Name) }}”

“- hosts: {{ hostvars.localhost.aws\_host\_target.msg”

This is also an error because the trailing “}}” is missing.

You can pass in host spec parameters with “-e” on the command line.

---

<div class="post-metadata">

**Author:** ![Shih\_Oon\_Liong2](https://avatars.discourse-cdn.com/v4/letter/s/90ced4/32.png) [@Shih\_Oon\_Liong2](https://forum.ansible.com/u/Shih_Oon_Liong2)\
**Post date:** [August 14, 2014, 5:46pm UTC](https://forum.ansible.com/t/geting-vars-from-a-different-playbook-to-use-as-the-hosts-value/15938/3 "2014-08-14T17:46:39Z")

</div>

Thanks! - that clarifies it for me.
