使用来自另一台主机的变量

lar*_*ybg 9 ansible

我有一个剧本,它在 Windows 机器上执行一个脚本,该脚本返回一个值,切换到localhost.
切换回后如何访问该值localhost

这是一个例子:

- 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] }}"
Run Code Online (Sandbox Code Playgroud)

从另一台主机访问变量的正确语法是什么?我收到错误消息:

"msg": "该任务包含一个带有未定义变量的选项。错误是:'windows' 未定义

lar*_*ybg 10

以下是适用于 a 中的组的代码loop

- name: print value_to_reuse
  debug:
    var: hostvars[item].value_to_reuse.stdout_lines
  loop: "{{ groups['windows'] }}"
Run Code Online (Sandbox Code Playgroud)

相同的代码无需迭代即可工作:

- name: print value_to_reuse
 debug:
   var: hostvars[groups['windows'].0].value_to_reuse.stdout_lines
Run Code Online (Sandbox Code Playgroud)