# Ansible - use variable from another host

**URL:** <https://forum.ansible.com/t/ansible-use-variable-from-another-host/26827>\
**Category:** Ansible Project\
**Tags:** windows\
**Created:** [February 22, 2018, 8:25pm UTC](https://forum.ansible.com/t/ansible-use-variable-from-another-host/26827 "2018-02-22T20:25:27Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Larry\_Bakun](https://avatars.discourse-cdn.com/v4/letter/l/41988e/32.png) [@Larry\_Bakun](https://forum.ansible.com/u/Larry_Bakun)\
**Post date:** [February 22, 2018, 8:25pm UTC](https://forum.ansible.com/t/ansible-use-variable-from-another-host/26827/1 "2018-02-22T20:25:27Z")

</div>

I have a playbook that executes a script on a Windows box that returns a value that I have to re-use later on in my playbook after switching to the localhost. How can I access this value after switching back to localhost. Here is an example:

`

hosts: localhost  
connection: local  
gather\_facts: no  
tasks:  
.  
.  
.  
hosts: windows  
gather\_facts: no  
tasks:

- name: Call PowerShell script  
win\_command: “c:\windows\system32\WindowsPowerShell\v1.0\powershell.exe c:\psl\_scripts\getData.ps1”  
register: value\_to\_reuse

hosts: localhost  
gather\_facts: no  
connection: local  
tasks:

- name: debug store\_name from windows host  
debug:  
var: “{{ hostvars[windows][value\_to\_reuse][stdout\_lines] }}”

`

```auto

```

What is the correct syntax accessing a variable from another host? I’m receiving error message: _“msg”: "The task includes an option with an undefined variable. The error was: ‘windows’ is undefined_

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [February 22, 2018, 8:42pm UTC](https://forum.ansible.com/t/ansible-use-variable-from-another-host/26827/2 "2018-02-22T20:42:22Z")

</div>

hostvars requires a string that is a host name: hostvars['\<hostname\>']  
, if the 'windows' is a hostname you can do  
hostvars['windows']['varname'] .. if it is a group , you cannot do  
this, you need to use a specific hostname.

But if you just want any host in the group you can do :  
hostvars[groups['windows'][0]]['value\_to\_reuse']['stdout\_lines']

Also note that if you don't quote something it will be interpreted as  
a variable that contains the name, not the variable name you want  
(list indecies are an exception as they are just numbers).
