14

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 localhost.
How can I access this value after switching back to localhost?

Here is an example:

- 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
  
  tasks:
    - name: debug store_name from windows host
      debug:
        var: "{{ hostvars[windows][value_to_reuse][stdout_lines] }}"

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

2 Answers 2

11

Here is the code that works for a group in a loop:

- name: print value_to_reuse
  debug:
    var: hostvars[item].value_to_reuse.stdout_lines
  loop: "{{ groups['windows'] }}"

Same code works without iterations:

- name: print value_to_reuse
 debug:
   var: hostvars[groups['windows'].0].value_to_reuse.stdout_lines
Sign up to request clarification or add additional context in comments.

1 Comment

This is a common need, yet I don't see this use case anywhere but here, even in the docs. For example, if you want to create a specific user on a single host, and access that users public SSH key so you can add it to all the rest of the hosts in the play, you would have to use this technique.
2

The syntax is:

- debug:
    var: hostvars['windows']['value_to_reuse']['stdout_lines']

Three mistakes:

  • you should quote string values
  • var parameter takes the variable name, not a template (which should be an msg-parameter value)
  • windows in the given example should be the host name as all facts are bound to hosts, not group of hosts

7 Comments

Hi @techraf. With your changes no red color in the output anymore, but still doesn't show any value: "hostvars['windows']['value_to_reuse']['stdout_lines']": "VARIABLE IS NOT DEFINED!"
I've executed script like this: hostvars['windows'] and got back only empty string: "hostvars['windows']": "", meaning there is no variables registered for "windows" host. How is this possible if I do register it?
Is your host names windows or a host group?
I'm connecting to a Windows Server, and do have a hosts file: [windows] 1.1.1.1 and groups_vars/windows.yml file with defined inventory variables as described here: docs.ansible.com/ansible/latest/intro_windows.html
Hi @techraf, this code works with with_items: - name: print value_to_reuse debug: var: hostvars[item]['value_to_reuse']['stdout_lines'] with_items: "{{ groups['windows'] }}"
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.