# trying to pass variable to hosts

**URL:** <https://forum.ansible.com/t/trying-to-pass-variable-to-hosts/24413>\
**Category:** Ansible Project\
**Created:** [May 7, 2017, 7:41pm UTC](https://forum.ansible.com/t/trying-to-pass-variable-to-hosts/24413 "2017-05-07T19:41:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![madhan0618](https://avatars.discourse-cdn.com/v4/letter/m/4af34b/32.png) [@madhan0618](https://forum.ansible.com/u/madhan0618)\
**Post date:** [May 7, 2017, 7:41pm UTC](https://forum.ansible.com/t/trying-to-pass-variable-to-hosts/24413/1 "2017-05-07T19:41:03Z")

</div>

I am trying to pass a variables to the hosts in a play

- name: include vars  
hosts: localhost  
connection: local  
tasks:

- include\_vars:  
dir: ‘../group\_vars’  
depth: 1

- name: debug  
hosts: localhost  
connection: local  
tasks:

- debug: msg=“{{ kfkahost0 }}”

- name: Create topic  
hosts: “{{ kfkahost0 }}”  
tasks:

# - name: include vars

# - include\_vars: dir=“../group\_vars” depth=“1”

#dir: ‘../group\_vars’  
#depth: 1

- shell: ./app/bin/kafka/bin/kafka-topics.sh --zookeeper {{ zkhost0 }}:2181 --create --topic {{ topic }} --partitions {{ partition }} --replication-factor {{ rpfactor }}  
42,3 Bot

in the debug step i see the hostname comes out in the msg fine

but in the next play it says kfkahost0 is undefined.

---

<div class="post-metadata">

**Author:** ![Dick\_Davies](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@Dick\_Davies](https://forum.ansible.com/u/Dick_Davies)\
**Post date:** [May 7, 2017, 11:30pm UTC](https://forum.ansible.com/t/trying-to-pass-variable-to-hosts/24413/2 "2017-05-07T23:30:35Z")

</div>

Those variables are set on localhost , not kfkahost0.

Why don't you just put your kafka host in a group and use group\_vars?

---

<div class="post-metadata">

**Author:** ![Jon\_Franklin](https://avatars.discourse-cdn.com/v4/letter/j/e19adc/32.png) [@Jon\_Franklin](https://forum.ansible.com/u/Jon_Franklin)\
**Post date:** [May 31, 2017, 1:12am UTC](https://forum.ansible.com/t/trying-to-pass-variable-to-hosts/24413/3 "2017-05-31T01:12:46Z")

</div>

Use hostvars, like: {{ hostvars['localhost']['kfkahost0'] }}
