Ansible - 是否可以注册多个变量?

Shi*_*kar 0 ansible ansible-2.x

我有一个 python 脚本,它正在返回/打印两个列表。

测试文件

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

我的剧本看起来像:

     -task:
      name: get the hosts
      command: /test.py
      register: result
Run Code Online (Sandbox Code Playgroud)

现在,是否可以从注册变量 result 中分别取出 pool1 和 pool2 ?如果是,请给我举个例子。

任何帮助或建议将不胜感激。

tec*_*raf 5

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

结果:

ok: [localhost] => {
    "msg": "Pool1 is [x.x.x.x, x.x.x.x] and Pool2 is [x.x.x.x, x.x.x.x]"
}
Run Code Online (Sandbox Code Playgroud)

根据您以后使用变量的方式,您可能/可能不需要from_json过滤(请参阅)。