# Hostvars giving  me undefined error

**URL:** <https://forum.ansible.com/t/hostvars-giving-me-undefined-error/32048>\
**Category:** Ansible Project\
**Created:** [January 24, 2020, 4:49pm UTC](https://forum.ansible.com/t/hostvars-giving-me-undefined-error/32048 "2020-01-24T16:49:04Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rakesh\_Parida](https://avatars.discourse-cdn.com/v4/letter/r/3ab097/32.png) [@Rakesh\_Parida](https://forum.ansible.com/u/Rakesh_Parida)\
**Post date:** [January 24, 2020, 4:49pm UTC](https://forum.ansible.com/t/hostvars-giving-me-undefined-error/32048/1 "2020-01-24T16:49:04Z")

</div>

Hi

Iam trying to access the variable from other hosts using hostvars. I am getting the undefined error.  
Can anybody help me: stuck at this point

- hosts: yum  
gather\_facts: false  
vars:  
file\_locn: “/san/repos/{{ version }}/packages”  
tasks:

- name: Find the Packages that are present in File location  
shell: ls -lrt {{ file\_locn }} |awk ‘NR\>1{print $9}’|grep -i rpm  
register: files\_present

- name: Set Fact  
set\_fact:  
some\_value: “{{ files\_present.stdout\_lines }}”

- debug:  
msg: “{{ some\_value }}”

- hosts: localhost  
gather\_facts: false  
tasks:

- name: Show hostvars  
debug:  
msg: “{{ hostvars[‘yum’][‘some\_value’] }}”

error:

PLAY [yum] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

TASK [Find the Packages that are present in File location] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\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\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

TASK [Show hostvars] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
fatal: [localhost]: FAILED! =\> {“msg”: “The task includes an option with an undefined variable. The error was: "hostvars[‘yum’]" is undefined\n\nThe error appears to be in ‘/home/rparida1/raka1111.yml’: line 20, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n tasks:\n - name: Show hostvars\n ^ here\n”}

PLAY RECAP \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
YUM01 : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0  
localhost : ok=0 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [January 24, 2020, 6:05pm UTC](https://forum.ansible.com/t/hostvars-giving-me-undefined-error/32048/2 "2020-01-24T18:05:35Z")

</div>

Is ‘yum’ a group name perhaps?

---

<div class="post-metadata">

**Author:** ![jean-yves](https://avatars.discourse-cdn.com/v4/letter/j/a587f6/32.png) [@jean-yves](https://forum.ansible.com/u/jean-yves)\
**Post date:** [January 24, 2020, 6:54pm UTC](https://forum.ansible.com/t/hostvars-giving-me-undefined-error/32048/3 "2020-01-24T18:54:34Z")

</div>

Hi,

You should better use the find module instead of shell module and ls.

As said, yum seem to be a group… To use hostvars special variable you need to use host and not group…

Something like this will work :

msg: “{{ hostvars[‘YUM01’][‘some\_value’] }}”

But instead of asking to remove your error, perhaps you could explain what you want to do…

Regards,

JYL

---

<div class="post-metadata">

**Author:** ![Rakesh\_Parida](https://avatars.discourse-cdn.com/v4/letter/r/3ab097/32.png) [@Rakesh\_Parida](https://forum.ansible.com/u/Rakesh_Parida)\
**Post date:** [January 24, 2020, 9:38pm UTC](https://forum.ansible.com/t/hostvars-giving-me-undefined-error/32048/4 "2020-01-24T21:38:43Z")

</div>

Yum is a hostname ina group variable  
And der are multiple yum server entries

---

<div class="post-metadata">

**Author:** ![Rakesh\_Parida](https://avatars.discourse-cdn.com/v4/letter/r/3ab097/32.png) [@Rakesh\_Parida](https://forum.ansible.com/u/Rakesh_Parida)\
**Post date:** [January 24, 2020, 9:39pm UTC](https://forum.ansible.com/t/hostvars-giving-me-undefined-error/32048/5 "2020-01-24T21:39:33Z")

</div>

I had already tried d same but same error

---

<div class="post-metadata">

**Author:** ![Rakesh\_Parida](https://avatars.discourse-cdn.com/v4/letter/r/3ab097/32.png) [@Rakesh\_Parida](https://forum.ansible.com/u/Rakesh_Parida)\
**Post date:** [January 24, 2020, 9:54pm UTC](https://forum.ansible.com/t/hostvars-giving-me-undefined-error/32048/6 "2020-01-24T21:54:20Z")

</div>



---

<div class="post-metadata">

**Author:** ![Rakesh\_Parida](https://avatars.discourse-cdn.com/v4/letter/r/3ab097/32.png) [@Rakesh\_Parida](https://forum.ansible.com/u/Rakesh_Parida)\
**Post date:** [January 24, 2020, 10:01pm UTC](https://forum.ansible.com/t/hostvars-giving-me-undefined-error/32048/7 "2020-01-24T22:01:21Z")

</div>

Hi Dick/Jean,

My inventory file has only one entry of yum…  
Now i have also tried to use :

- name: Show hostvars  
debug:  
msg: “{{ hostvars[groups[‘yum’][‘some\_value’] }}”

But still i get same error  
I think iam doing some error in syntax…  
as i keep getting the undefined error

Pls suggest

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [January 25, 2020, 10:25pm UTC](https://forum.ansible.com/t/hostvars-giving-me-undefined-error/32048/8 "2020-01-25T22:25:40Z")

</div>

You playbook only takes into account "localhost", as a result any  
other hostvars will be undefined.
