# Passing a variable into 'hosts' in the same playbooks

**URL:** <https://forum.ansible.com/t/passing-a-variable-into-hosts-in-the-same-playbooks/24276>\
**Category:** Ansible Project\
**Tags:** rhel, aws\
**Created:** [April 21, 2017, 5:35am UTC](https://forum.ansible.com/t/passing-a-variable-into-hosts-in-the-same-playbooks/24276 "2017-04-21T05:35:44Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tony\_Xynidakis](https://avatars.discourse-cdn.com/v4/letter/t/ce7236/32.png) [@Tony\_Xynidakis](https://forum.ansible.com/u/Tony_Xynidakis)\
**Post date:** [April 21, 2017, 5:35am UTC](https://forum.ansible.com/t/passing-a-variable-into-hosts-in-the-same-playbooks/24276/1 "2017-04-21T05:35:44Z")

</div>

Hi

Sorry I’m coding and learning at the same time

I’m writing a playbook to generate an AMI in ansible

basic steps of the playbook

1. launch an EC2 instance in AWS  
1.a wait for ssh port to come up
2. refresh cached output from ec2.py
3. build the instance with a role
4. generate the AMI  
4.a terminate running instance

I’ve got this working with 3 playbooks

1. Launch instance
2. Build instance
3. create AMI (includes termination)

I would like to combine all the above in 1 playbook

the issue I have is passing in the hosts as a variable after I refreshed the ec2.py cache

the area on the code I’m focusing on is

…

- set\_fact:  
target\_host: “tag\_Name\_RHEL-base-{{ instance\_date }}-01”

- debug: var=target\_host

- name: Build RHEL Base AMI image  
hosts: “{{ target\_host }}”  
#hosts: tag\_Name\_RHEL-base\*  
connection: local  
gather\_facts: False  
tasks:

- debug: msg=“Hostname is {{ target\_host }}”

The error is  
TASK [set\_fact] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost]

TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> {  
“target\_host”: “tag\_Name\_RHEL-base-20170421-01”  
}  
**ERROR! the field ‘hosts’ has an invalid value, which appears to include a variable that is undefined. The error was: ‘target\_host’ is undefined**

The error appears to have been in ‘/home/opentext/playbooks/launch\_base\_RHELAWSinstance.yml’: line 71, column 3, but may  
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

- name: Build RHEL Base AMI image  
^ here

Any suggestions to solving the issue?

Kind Regards  
Tony

---

<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:** [April 24, 2017, 2:30pm UTC](https://forum.ansible.com/t/passing-a-variable-into-hosts-in-the-same-playbooks/24276/2 "2017-04-24T14:30:15Z")

</div>

You need to specify from which you are fetching the variable from.

{{ hostvars.localhost.target\_host }}
