Ansible-是否可以注册多个变量?
我有一个python脚本,它返回/打印两个列表 test.pyAnsible-是否可以注册多个变量?,ansible,ansible-2.x,Ansible,Ansible 2.x,我有一个python脚本,它返回/打印两个列表 test.py def getHosts(): Pool1=[x.x.x.x, x.x.x.x] Pool2=[x.x.x.x, x.x.x.x] Print pool1,pool2 Return pool1,pool2 getHosts() 我的剧本看起来像: -task: name: get the hosts command: /test.p
def getHosts():
Pool1=[x.x.x.x, x.x.x.x]
Pool2=[x.x.x.x, x.x.x.x]
Print pool1,pool2
Return pool1,pool2
getHosts()
我的剧本看起来像:
-task:
name: get the hosts
command: /test.py
register: result
现在,是否可以从注册的变量结果中分别取出pool1和pool2?如果是,请给我举个例子
任何帮助或建议都将不胜感激。生成JSON并将其提供给Ansible。它将自动创建适当的数据结构:
---
- hosts: localhost
gather_facts: no
connection: local
tasks:
- command: 'echo { \"Pool1\": \"[x.x.x.x, x.x.x.x]\", \"Pool2\": \"[x.x.x.x, x.x.x.x]\" }'
register: my_output
- set_fact:
my_variable: "{{ my_output.stdout | from_json }}"
- debug:
msg: "Pool1 is {{ my_variable.Pool1 }} and Pool2 is {{ my_variable.Pool2 }}"
结果:
ok: [localhost] => {
"msg": "Pool1 is [x.x.x.x, x.x.x.x] and Pool2 is [x.x.x.x, x.x.x.x]"
}
取决于您以后如何使用该变量,您可能/可能不需要从_json过滤器(请参阅)生成json并将其提供给Ansible。它将自动创建适当的数据结构:
---
- hosts: localhost
gather_facts: no
connection: local
tasks:
- command: 'echo { \"Pool1\": \"[x.x.x.x, x.x.x.x]\", \"Pool2\": \"[x.x.x.x, x.x.x.x]\" }'
register: my_output
- set_fact:
my_variable: "{{ my_output.stdout | from_json }}"
- debug:
msg: "Pool1 is {{ my_variable.Pool1 }} and Pool2 is {{ my_variable.Pool2 }}"
结果:
ok: [localhost] => {
"msg": "Pool1 is [x.x.x.x, x.x.x.x] and Pool2 is [x.x.x.x, x.x.x.x]"
}
根据您以后如何使用该变量,您可能/可能不需要从_json过滤器(请参阅)中执行
。为什么不直接生成一个json对象?@techraf您的意思是从python返回json?是的,我可以这样做,但如何从寄存器变量读取JSON对象。(ansible非常新,请分享一些代码)你说的“如何阅读”是什么意思?如果我喜欢register:result,其中result是JSON。那么如何从Playbook中的'result'变量中读取对象为什么不简单地生成一个JSON对象?@techraf你的意思是说从python返回一个JSON?是的,我可以这样做,但如何从寄存器变量读取JSON对象。(ansible非常新,请分享一些代码)你说的“如何阅读”是什么意思?如果我喜欢register:result,其中result是JSON。然后如何从playbook中的“result”变量中读取对象