本文介绍了Ansible - 使用来自另一个主机的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个剧本,它在 Windows 机器上执行一个脚本,该剧本返回一个值,我必须在切换到本地主机后在我的剧本中重新使用该值.切换回本地主机后如何访问此值.下面是一个例子:
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] }}"
从另一台主机访问变量的正确语法是什么?我收到错误消息:"msg": "该任务包含一个带有未定义变量的选项.错误是:'windows' 未定义
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
推荐答案
这是有效的代码with_items
:
- name: print value_to_reuse
debug:
var: hostvars[item]['value_to_reuse']['stdout_lines']
with_items: "{{ groups['windows'] }}"
相同的代码无需迭代即可工作:
Same code works without iterations:
- name: print value_to_reuse
debug:
var: hostvars[groups['windows'][0]]['value_to_reuse']['stdout_lines']
这篇关于Ansible - 使用来自另一个主机的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!