# 'hostvars' is undefined

**URL:** <https://forum.ansible.com/t/hostvars-is-undefined/25703>\
**Category:** Ansible Project\
**Created:** [October 9, 2017, 2:08pm UTC](https://forum.ansible.com/t/hostvars-is-undefined/25703 "2017-10-09T14:08:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![KP1](https://avatars.discourse-cdn.com/v4/letter/k/ea666f/32.png) [@KP1](https://forum.ansible.com/u/KP1)\
**Post date:** [October 9, 2017, 2:08pm UTC](https://forum.ansible.com/t/hostvars-is-undefined/25703/1 "2017-10-09T14:08:09Z")

</div>

Encountered below error:

ERROR! the field ‘hosts’ has an invalid value, which appears to include a variable that is undefined. The error was: ‘hostvars’ is undefined  
I am using below command to define hosts value:

- hosts: “{{ ‘prod-app-servers’ if hostvars[‘127.0.0.1’].result.stdout == ‘prod’ else ‘dr-app-servers’ }}”

I have tried different possibilities but of no use:

- hosts: “{{ ‘prod-app-servers’ if hostvars[‘127.0.0.1’][result].stdout == ‘prod’ else ‘dr-app-servers’ }}”

I have tried below jinja format aswell, but of no use…

{% if {{hostvars[127.0.0.1][result][stdout]}} == prod %}prod-app-servers{% else %}dr-app-servers{% endif %}

My intention is to decide host during runtime based on a result.

I am not sure what is the mistake. prod-app-servers & dr-app-servers are the host groups that i defined in inventory file. ‘result’ is the value that gets populated at the begining of the playbook execution:

- hosts: 127.0.0.1  
connection: local  
gather\_facts: no  
tasks:
- name: Check Failover or Failback  
command: --Executing a script file with some parameters–  
register: result

It fails on the syntax check, however the playbook run fine as expected.

---

<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:** [October 9, 2017, 5:06pm UTC](https://forum.ansible.com/t/hostvars-is-undefined/25703/2 "2017-10-09T17:06:22Z")

</div>

This will only work if both play is in the same playbook, you error message indicate that this is not the case.

---

<div class="post-metadata">

**Author:** ![KP1](https://avatars.discourse-cdn.com/v4/letter/k/ea666f/32.png) [@KP1](https://forum.ansible.com/u/KP1)\
**Post date:** [October 10, 2017, 9:32am UTC](https://forum.ansible.com/t/hostvars-is-undefined/25703/3 "2017-10-10T09:32:36Z")

</div>

Hi,  
Thanks for your reply.

All the plays are in same playbook.

My script is something like below:

- hosts: 127.0.0.1  
connection: local  
gather\_facts: no  
tasks:

- name: Execute script  
command:   
register: result

- hosts: “{{ ‘prod-app-servers’ if hostvars[‘127.0.0.1’].result.stdout == ‘prod’ else ‘dr-app-servers’ }}”  
gather\_facts: no  
tasks:

- name: Stop IIS  
win\_command: iisreset /stop

As mentioned earlier, my script works as expected during execution but it throws error during syntax check.

Thanks
